Help with Active and WaitForSeconds!

I need to make a Gameobject unactive when the levels starts (Fire.Active = false) and then if i press a button,it will become Active for 0.2 seconds and then become Unactive again.

This is what I’ve got so far and It doesn’t works at all.

var Fire : GameObject;
var Fire_S : AudioClip;

function Start() {

  if (Input.GetKeyDown (KeyCode.E)) {


    Fire.active = true;
    yield WaitForSeconds (0.2);
    Fire.active = false;
    audio.PlayOneShot(Fire_S, 1);

}
}

I know I’m doing something wrong,please help me :[

#pragma strict

var Fire : GameObject;
 //var Fire_S : AudioClip;
 
 function Start() {
 	Fire.active = false;
 }
 function Update()
 {
	 AnotherFunction();
 }
 function AnotherFunction()
 {
 	if (Input.GetKeyDown (KeyCode.E)) {
	     Fire.active = true;
	     yield WaitForSeconds (0.2);
	     Fire.active = false;
	     //audio.PlayOneShot(Fire_S, 1);
	 }
 }

You have two ways:

  • Coroutine : (Recommended)

    var Fire : GameObject;
    //var Fire_S : AudioClip;

    function Start() {
    Fire.active = false;
    }
    function Update()
    {
    if (Input.GetKeyDown (KeyCode.E)) {
    FireEffect();
    }
    }

    // Co-routine function
    function FireEffect()
    {
    Fire.active = true;
    yield WaitForSeconds (0.2);
    Fire.active = false;
    //rest of the stuff here
    }
    }

  • Invoke function

      function Start() {
        Fire.active = false;
    }
    
    function Update()
    {
         if (Input.GetKeyDown (KeyCode.E)) {
                    EnableFireEffect();
         }
    }
    
    function EnableFireEffect()
    {
            Fire.active = true;
            Invoke("DisableFireEffect", 0.2f);
        }
    }
    
    function DisableFireEffect()
    {
            Fire.active = false;
            //rest of the stuff here
        }
    }