Bug信息:
MissingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
代码块:
private Button btn_Restart;
private void Awake()
{
btn_Restart = transform.Find("Bg/text_restart").GetComponent<Button>();
btn_Restart.onClick.AddListener(OnRestart);
}
private void Show()
{
txt_Score.text = GameManager._instance.GetGameScore().ToString();
txt_AddDiamondCount.text =
"+"+GameManager._instance.GetGameDiamond().ToString();
gameObject.SetActive(true);
}//每次切换场景要更新的信息
private void OnRestart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);//切换场景
}
根据错误信息应该是每一次在切换场景之后它都会把物体都销毁掉了,以至于unity找不到物体的信息,但是博主的代码块里没有销毁的代码块,所以博主判断应该是和切换场景有关。
于是我在更新信息的代码块里进行了安全校验,这样就不会报错啦
private void Show()
{
if(txt_Score!=null&&txt_AddDiamondCount!=null)//添加了一个安全校验
{
txt_Score.text = GameManager._instance.GetGameScore().ToString();
txt_AddDiamondCount.text = "+"+GameManager._instance.GetGameDiamond().ToString();
gameObject.SetActive(true);
}
}
|