在用Unity 3d UGUI练习的时候,老是报NullReferenceException: Object reference not set to an instance of an object
InputUserPassword.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InputUserPassword : MonoBehaviour
{
public int Password;
// Start is called before the first frame update
void Start()
{
transform.GetComponent().onValueChanged.AddListener(Change_Value);
transform.GetComponent().onEndEdit.AddListener(End_Value);
}
// Update is called once per frame
void Update()
{
}
public void Change_Value(string inputPassword)
{
print("正在输入" + inputPassword);
}
public void End_Value(string inputPassword)
{
print("密码" + inputPassword);
Password = int.Parse(inputPassword);
}
}
InputButton.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoginButton : MonoBehaviour
{
public GameObject InputUserPassword;
public GameObject InputUserName;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void ClickButton()
{
if (InputUserPassword.GetComponent().Password > 0)
{
SceneManager.LoadScene(1);
}
else
{
print("用户名或密码不能为空");
}
}
}
反复实验,没有找到方法
用户名和密码可以判断是否为空
典型的报空引用的错呀,在你代码的22行,检查一下是不是没有给哪个对象赋值,或者是找不到相关对象就报空了
看起来应该是InputUserPassword物体没有在inspector面板上拖拽赋值,导致下面这行报错: