for loop not working

Hello, I don’t know why my loop isn’t working :frowning: I think it should run ten times:

var timer: float = 0.5; // set duration time in seconds in the Inspector
function Update()
{ for (var i = 1; i < 10; Debug.Log("Wolfi") ){
    i = i + 1;
            timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
     
            if (timer > 0)
            {} 
            else // timer is <= 0
            {
                if (gameObject.active)
                {gameObject.SetActive(false);}
                else {gameObject.SetActive(true);}
            }
        }}

Remove the for-loop from Update. Do you really want a for-loop loop running 10 times every frame?

Add a reference of a GameObject to enable/disable, because disabling the GameObject will also stop the script from Updating,

Try something like this instead. Make the siren/light a child GameObject and place this script on the parent.

  • ParentGameObject (place script here)
  • ChildGameObject (light)

.

 var timer: float = 0.5; // set duration time in seconds in the Inspector

 public var targetObject : GameObject; // a child GameObject
 private var elapsedTime : float = 0.0f;
 
function Update()
{ 
    elapsedTime += Time.deltaTime;
    if (elapsedTime > timer) {
        targetObject.SetActive(!targetObject.active);
        elapsedTime = elapsedTime % timer;
    } 
}