将文本对象分配给预制件时遇到问题。生成预制件后,文本对象消失了。试图分配为GameObject,但无法转换为UI.Text
public class Enemy : MonoBehaviour
{
public int currentlife;
GameObject enemySpawner;
private float timePassed;
public Text LifeNow;
GameObject hpBar;
Slider healthBar;
private void Start()
{
enemySpawner = GameObject.FindWithTag("Spawner");
currentlife = 12;
hpBar = GameObject.FindWithTag("TagForSlider");
healthBar = hpBar.GetComponent<Slider>();
}
private void Update()
{
if (currentlife <= 0)
{
this.GetComponent<Animator>().SetTrigger("death");
GameObject.Find(gameObject.name + ("spawn point")).GetComponent<Respawn>().Death = true;
timePassed += Time.deltaTime;
if (timePassed >= 0.5)
{
Destroy(gameObject);
}
}
healthBar.value = currentlife;
LifeNow.text = "Health: " + currentlife;
}
public void TakeDamage(int damage)
{
currentlife -= damage;
}
}
答案 0 :(得分:2)
您有一个公共的Text LifeNow变量。您需要获取预制的文本对象并将其分配给变量,然后才能使用LifeNow.text
更新文本。例如LifeNow = PREFAB.GetComponent<Text>();
在回答这个问题之前,我会问过您是否在评论中这样做,但是到目前为止,我还没有足够的代表发表评论。