- Home /
Function action applies to every object that have script with that function on it. Help please
Hi, I made script that causes that object that have that script explodes after some damage. Problem is, that i have many of these objects in scene end when one object takes the damage, one other in scene explodes. I don'n know where i made a mistake. Thanks for help.
Here is the script:
var maxHealth : float; static var currentHealth : float;
var dieSound : AudioClip; var dieVisual : GameObject;
function Start() { currentHealth = maxHealth; }
function Update() { if(currentHealth <= 0) { Destroy(); } }
function Destroy() { AudioSource.PlayClipAtPoint(dieSound, transform.position); Instantiate(dieVisual, transform.position, transform.rotation); Destroy(gameObject); }
Answer by ScroodgeM · Aug 17, 2012 at 10:52 AM
static variables are stored globally, not per instance. so make it non-static (remove static keyword)
you:
removed static keyword from 2nd line of script
saved script
returned to unity and let unity recompile scripts
run game again
noticed that on one die all objects dies too?
when there was static, all objects died at once, when i removed it, objects destroy one by one, but by order they are placed in hierarchy.
then try to localize point that starts destroying. before you'd remove static it was caused by all objects have shared health and once it becomes <=0, all objects are destroyed. now there's something else goes wrong.
check health of other objects - don't it goes to zero?
if they dies with sound and visual effects, something runs Destroy() method - where else this method can be raised?
etc
It destroys by order in hirearchy only objects from the same prefab. I still didn't figured out what's the problem..
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 1 Answer
What function to use, for this statement? 1 Answer
Make an object move towards random spot on another objects edge? 1 Answer
"transform" in a static function 1 Answer
how to put in function any variables? like object? 2 Answers