Проект работает нормально, пока не будет достигнута определенная точка, а затем вдруг он начнет выбрасывать NRE. Вот некоторый исходный код:
void Start(){
myhealth = GetComponentInChildren<HealthBar>();
if(myhealth == null)
{
Debug.Log("myhealth is null !!"); //It never outputs something here
}
}
//And Here it works :
public void ApplyDamage(float amount)
{
myhealth.DamageEnemy(amount);
if (GetHealth() <= 0)
{
[...]
}
}
//Then suddenly it throws NRE's here when accesing it from another Script :
public void AddHealth(float a)
{
myhealth.HealEnemy(a); //Here
}
public float GetHealth()
{
return myhealth.GetHealth(); //And here
}
В скрипте HealthBar есть эти переменные и эти функции:
public float maxHealth;
public float currentHealth;
private float originalScale;
public void DamageEnemy(float giveDamage)
{
currentHealth -= giveDamage;
}
public void HealEnemy(float heal)
{
currentHealth += heal;
}
public float GetHealth()
{
return currentHealth;
}
Кажется, нет причин для того, чтобы сценарий выдавал NRE, но он все же это делает.
HealthBar
скрипт - person Programmer   schedule 02.10.2016