# How to enable/ disable GameObject

I have child object with ParticleSystem component. I want to have particles disabled until I press button.

I unchecked child object to make it disabled but when I press play it enables automatically(with no scripts attached). Then I made this script below (with .SetActive(false) part only) and it worked.

But then I needed to activate it somehow, which I did, but now when I press play, object is automatically enabled/ activated again.

Is there another way of doing this because I need it haha.

var particle1: GameObject;
var particle2: GameObject;
var particle3: GameObject;

public var triggered : boolean = false;

function OnTriggerEnter() { triggered= true; }
function OnTriggerExit()  { triggered= false;}

function Start()
{
particle1.SetActive(false);
particle2.SetActive(false);
particle3.SetActive(false);
}

function Update()
{
if (triggered && Input.GetKeyDown(KeyCode.JoystickButton1))
particle1.SetActive(true);
particle2.SetActive(true);
particle3.SetActive(true);
}

• I don't know if it is intended, but in your Update() there is no brackets on the three following lines, which means that at each loop, particle2 and particle3's SetActive(true) will be called. – Alexandre Desbiens Aug 4 '14 at 14:40
• Yeah, I want all three particles to run when object is activated – Samurai Fox Aug 4 '14 at 14:42
• No, not in the Start function, in the Update one, with the Input.getKeyDown(...). The Update() function is called many times a second, so particle2 and particle3 will be enabled again on each loop. – Alexandre Desbiens Aug 4 '14 at 14:44
• it was the curly brackets in Update() func. that were the problem...thanks anyways – Samurai Fox Aug 4 '14 at 14:45

It might be related to your Update function:

function Update()
{
if (triggered && Input.GetKeyDown(KeyCode.JoystickButton1))
particle1.SetActive(true);
particle2.SetActive(true);
particle3.SetActive(true);
}


Having no brackets, only particle1.setActive(true) is in the condition. The other two will be called on each Update() loop. Try adding brackets and see if the problem persists:

function Update()
{
if (triggered && Input.GetKeyDown(KeyCode.JoystickButton1))
{
particle1.SetActive(true);
particle2.SetActive(true);
particle3.SetActive(true);
}
}

• yap...that was it...thanks I'm actually kind of ashamed I didn't think of that haha – Samurai Fox Aug 4 '14 at 14:44
• No problem. Small errors like this happens even to the best programmers (i'm talking about you, semicolon). – Alexandre Desbiens Aug 4 '14 at 14:45