Turn on object after a second

I’m trying to turn on a light after waiting a second. There are no complier errors, but when I play the game, the boolean never turns on. If someone could help me out with what I’m doing wrong that would be much appreciated. Thanks.

#pragma strict

var bluelight : GameObject;

var bluelightz : boolean = false;

function Start ()
{

if(bluelightz==true)
{
bluelight.SetActive (true);
}

if(bluelightz==false)
{
    yield WaitForSeconds (1);
    bluelight.SetActive (true);
}
}

Hi,

You have bluelight.SetActive(true) in both whatever the value of boolean is, it doesnot matter. Your blue light will stay active. And the bool never get true because you are not making it true at any point. Its value is false only.

#pragma strict

 var bluelight : GameObject;
 
 function Start ()
 {
 	if(bluelight.active == false )
 	{
     	yield WaitForSeconds (1);
     	bluelight.active = true;
 	}
 }

or

#pragma strict

 var bluelight : GameObject;
 
 function Start ()
 {
 	if(bluelight.GetComponent(Light).enabled == false )
 	{
     	yield WaitForSeconds (1);
     	bluelight.GetComponent(Light).enabled = true;
 	}
 }

Try like this:

var bluelight : GameObject; 
var bluelightz : boolean = false;
 
function Start ()
{
    // reset blue light
    if(bluelightz == true)
    {
       bluelightz = false;
       bluelight.SetActive (false); // should change to false
    }

    StartCoroutine(TurnOn());

}

function TurnOn()
{
    if(bluelightz == false)
    {
       yield WaitForSeconds (1);
       bluelightz == true;
       bluelight.SetActive (true);
    }
}

I used Coroutine to fix this problem.

Hope it helps!