x


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);
} 
more ▼

asked Aug 17 '12 at 10:51 AM

Arlei gravatar image

Arlei
16 1 2 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

static variables are stored globally, not per instance. so make it non-static (remove static keyword)

more ▼

answered Aug 17 '12 at 10:52 AM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

I removed it, but it didn't help..

Aug 17 '12 at 11:03 AM Arlei

you:

  1. removed static keyword from 2nd line of script

  2. saved script

  3. returned to unity and let unity recompile scripts

  4. run game again

  5. noticed that on one die all objects dies too?

Aug 17 '12 at 11:13 AM ScroodgeM

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.

Aug 17 '12 at 12:18 PM Arlei

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

Aug 17 '12 at 01:52 PM ScroodgeM

It destroys by order in hirearchy only objects from the same prefab. I still didn't figured out what's the problem..

Aug 17 '12 at 03:42 PM Arlei
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3335
x1095
x573
x478
x21

asked: Aug 17 '12 at 10:51 AM

Seen: 261 times

Last Updated: Aug 17 '12 at 03:55 PM