Im calling a variable from another script and added 50 to my CurrentMoney when i destroy the rigidbody , but this comes up …
Script :
function Start () {
CurrentHealth = MaxHealth;
}
function ApplyDamage (Damage : float) {
if(CurrentHealth < 0){
return;
}
CurrentHealth -= Damage;
if(CurrentHealth == 0){
Destroy( gameObject );
var MoneyScript: MoneyScript = GetComponent(MoneyScript);
MoneyScript.CurrentMoney + 50();
}
}
ive done alot of research for the past half an hour or hour but still cant find any soloution
This bit will cause problems sooner or later: var MoneyScript: MoneyScript = GetComponent(MoneyScript); MoneyScript.CurrentMoney + 50(); You should ensure that you keep your variable and class names distinct: var moneyScript: MoneyScript = GetComponent(MoneyScript); moneyScript.CurrentMoney + 50();
– KellyThomasWill also cause problems because it's trying to access a component of the just destroyed game object...
– tanoshimiTanoshimi, Since 'Destroy' doesn't actually destroy the object until the end of the current frame, it should still work, but it's an interesting thing to note that, since the GameObject will be destroyed, what is the point of incrementing a value on it. Good catch.
– iwaldropIf the health is less that zero, this method returns. If greater than zero it applies damage, then checks if health is exactly zero to see if it should call Destroy(). i.e. the object is only destroyed if a hit places it at exactly zero health.
– KellyThomas