- Home /
Instantiating Prefab Every 5 seconds
Hey,
I am new to Unity and trying to do a college assignment...
I need to repeat a game object which I have animated and got to bounce along the screen etc...
I am finding it hard to instantiate the prefab every 5 seconds or so so the characters can keep repeating...
I have searched code but every time I put it into a script I get error...
Can anyone help me out please? Do certain parts of the code have to match parts of my prefab or gameobject?
Thanks. John
Answer by YoungDeveloper · Dec 02, 2014 at 12:15 PM
There are severall solutions to achieve this. First few which come to my mind are Update, Coroutine or InvokeRepeat. http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html
Coroutine Example.
void Start(){
StartCoroutine(Spawner());
}
public IEnumerator Spawner(){
bool flag = true;
while(flag){
Debug.Log("Bam");
yield return new WaitForSeconds(5f);
}
}
Use Instantiate() to create desired gameObject.
http://docs.unity3d.com/ScriptReference/Object.Instantiate.html