C# 运行状况文本离开对象引用未设置为每帧对象的实例
所以…我的问题是healthText.text=“health:”+health 我的控制台说对象引用未设置为对象的实例。 很抱歉,代码太糟糕了,但我只对代码感兴趣2.5周,所以,请不要把眼睛伸出来C# 运行状况文本离开对象引用未设置为每帧对象的实例,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,所以…我的问题是healthText.text=“health:”+health 我的控制台说对象引用未设置为对象的实例。 很抱歉,代码太糟糕了,但我只对代码感兴趣2.5周,所以,请不要把眼睛伸出来 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerDED : MonoBehaviour
{
public int Health = 100;
public int damage = 2;
public Text healthText;
public bool AlreadyTook = false;
// Start is called before the first frame update
void Update(){
Die();
healthText.text = "Health : " + Health.ToString();
}
void TakeDamage(){
Health -= damage;
}
public void Die(){
if(Health <= 0){
Debug.Log("SceneManager.LoadScene(Dead);)");
}
}
void OnCollisionEnter2D(Collision2D col ){
if(col.collider.tag == "Enemy" && AlreadyTook == false){
AlreadyTook = true;
TakeDamage();
}
}
void OnCollisionExit2D(Collision2D col){
if(col.collider.tag == "Enemy"){
AlreadyTook = false;
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SceneManagement;
使用UnityEngine.UI;
公共课扮演者:单一行为
{
公共卫生=100;
公共int损害=2;
公共文本;
公共布尔值AlreadyTook=false;
//在第一帧更新之前调用Start
无效更新(){
模具();
healthText.text=“Health:+Health.ToString();
}
无效损害(){
健康-=损害;
}
公共空间(){
如果(Health您在顶部声明了public Text healthText;
,但您从未用任何东西初始化它,那么当您尝试为Text
属性分配一个值时,您会得到对象引用错误。这就像询问某人的朋友,但从未指定某人
尝试使用Text
的实例初始化healthText
。我不熟悉该类型,但可能就是这样:healthText=newtext()
我不太清楚你的意思,我在unity中工作,所以我声明了需要放在编辑器中的文本。我对记分板也做了完全相同的操作,它没有给出任何错误,这让我更加困惑。首先,设置一个断点并在调试模式下运行游戏,这样你就可以逐步通过h代码。你可以用鼠标光标在变量名上看哪一个没有初始化。也许你删除了编辑器中的文本,它留下了代码片段?谁知道呢。