- Home /
How to Spawn a Gameobject According to a Timer?
Hi, Im making a game where I want, in the first wave for 10 enemies to spawn (Get activated) as the game starts, and then 20 more after 5 minutes has gone by. I've tried to figure it out, and was unsuccessful. Any help would be awesome! :)
function Start()
{
gameObject.SetActive(false);
}
function Update () {
if (Time.time > 300)
{
gameObject.SetActive(true);
}
}
Answer by robertbu · Jan 20, 2014 at 11:23 PM
When you disable a game object, you no longer receive Update() calls. So any solution that uses Update() will fail. One solution is to use a boolean flag rather than disabling the game object. But if you must disable the game object, Invoke() still works with disabled game objects. You can do:
function Start() {
Invoke("Reenable", 300);
gameObject.SetActive(false);
}
function Reenable() {
gameObject.SetActive(true);
}
@robertbu Thank you so much! it works perfectly :)
Answer by Exalia · Jan 20, 2014 at 11:13 PM
This should get the effect you want
public GameObject spawner
float timer = 300;
float time = 0;
function Start()
{
spawner.SetActive(false);
}
function Update()
{
if(time >= timer)
{
spawner.SetActive(true);
}
time += Time.deltaTime;
}
I hope this helps, let me know how you get on
I really appreciated the reply, but I figured it out. :) Thanks!
Your answer
Follow this Question
Related Questions
FPS Controller falls through 3D Model 1 Answer
radom spawn points 1 Answer
Need some help with Enemy Spawning Script... 1 Answer
How to fix Unknown identifier: 'bullitPrefab'? 1 Answer
Android 4.3 1 Answer