Deactivate An Object After Certain Amount of Time

Hi, I am using a script in which your weapon is active if you pass through a certain zone, but I would like that you only have it available for a certain amount of time and then it would deactivate until you pass through that zone again.

I’m thinking of something like:

static var minigunUnlocked = false;

if (minigunUnlocked == true)
time active = 30;

if time active = 0;
minigunUnlocked == false;

I think that something with that logic would work, but how could I actually implement it?

Hey Dude,

To do this you would first need to put this code in the Update() function of your script.

Then the logic is pretty much the same as you have it with the only addition being the timer countdown.

Something like this :

// This will let you configure your gun script from the editor 
public float minigunTimeout = 30F;

// Private stuff to make the activation timer work
private float _minigunTimeRemaining;
private bool _minigunActive = false;

private void Start()
   // Init our time remaining
   _minigunTimeRemaining = minigunTimeout;

// Inside your zone trigger, perhaps OnTriggerEnter()
 _minigunActive = true;

private void Update()

 if(_minigunActive && _minigunTimeRemaining > 0)
  // Reduce the remaining time by time passed since last update (frame)
  _minigunTimeRemaining -= Time.deltaTime;
  // The gun is now disabled and the timer is reset
  _minigunTimeRemaining = minigunTimeout;
  _minigunActive = false;


// If you have a Shoot function then perhaps you would do 

// this to check if the gun can actually shoot

private void Shoot()


 // Shoot code here


Everything is in C# because i cant remember the syntax for JS sorry :smiley:


easy, use Invoke and specify the time in secs:

Invoke("HideShowGameobject", 5);

create a new method called HideShowGameobject:

void HideShowGameobject()
             if (
    } = true;when weapon is active then use WaitForSeconds(); which will allows you a timer and after that just do = false;

Thanks to all of you, I read the article about static and I think I will start replaicing it for “Get.Component” :D, also thanks for the quick responses, for just a simple question. Hope you have a nice day.