我將殭屍預製件附加到殭屍對象;然而,當我按下Play時,腳本會在殭屍對象上消失。我按Play時如何確保腳本保留在對象上?統一:當進入播放模式時,對象上的腳本消失
我宣佈我的變量並初始化它。我知道這是基本的東西,但您的幫助將不勝感激!
public class PlayerLifeCollider : MonoBehaviour {
public Zombie zombie; //declared
public float damage = 1;
public void Start()
{
zombie = GetComponent<Zombie>(); //instantiated
}
private void Update()
{
zombie.DamagePlayer(damage); //**Null error here**
}
}
public class Zombie : MonoBehaviour
{
public int currentHealth;
private Player player;
private PlayerLifeCollider playerCollider;
public int damage;
public void Start()
{
playerCollider = GetComponent<PlayerLifeCollider>();
}
public void Damage(int damageAmount)
{
currentHealth -= damageAmount;
if (currentHealth <= 0)
{
playerCollider.ObjectsInRange.Remove(gameObject);
gameObject.SetActive(false);
}
}
public void DamagePlayer(float damage)
{
player.Life(damage);
}
}
public class Player : MonoBehaviour {
public float playerLife = 100;
public void Life (float damage)
{
playerLife -= damage;
if (playerLife <=0)
{
playerLife = 100;
SceneManager.LoadScene(2);
}
}
'player.Life(float)'的實現是什麼? –
如果在檢查器中已經分配了'zombie = GetComponent()',則不需要指定'zombie = GetComponent ()'。 –
ryeMoss