- Home /
Question by
francescowltp · Oct 26, 2013 at 08:30 PM ·
unity 4prefabey height
How to instantiate a Prefab in a random range in 5 secs ?
Hallo everyone, i just want to instantiate a prefab every 5 seconds, y position = 0 x position = Random.Range (-10;10) As in this image my prefab will be the parachutist
How can i do? Thanks in advance :)
giochi-puzzle-online-cannon-bods.jpg
(21.3 kB)
Comment
Answer by samtperrin · Oct 26, 2013 at 08:45 PM
I would use a coroutine to achieve that functionality. I recommend reading this link.
Here is what you could do:
private bool bCanCreateParachuter = true; // bool to stop the spawning
void Start()
{
StartCoroutine("CreateParachuter");
}
IEnumerator CreateParachuter()
{
while(bCanCreateParachuter)
{
Instantiate(prefab, new Vector3(Random.Range(-10,10), 0, 0), Quaternion.identity);
yield return new WaitForSeconds(5f);
yield return null;
}
yield return null;
}
Your answer
Follow this Question
Related Questions
How to get down character 1 Answer
Forcing OpenGL in Windows Editor 1 Answer
How to make CrossFade between 2 camera? 0 Answers
How do I randomize 5 prefabs on Scene loaded? 1 Answer
How Can I Reduce Build Time for Script-Heavy Projects? 2 Answers