- Home /
 
               Question by 
               user-4788 (google) · Sep 29, 2010 at 08:39 AM · 
                instantiaterandomcoroutinetime  
              
 
              instantiating at a random time
trying to instantiate a prefab at a random time.Do I use coroutine to stop the instantiated object for a few seconds or is there any other way?I would be very grateful if someone could edit my code because I am a new unity/C# user.Thanks
using UnityEngine; using System.Collections;
 
               public class aeroplane_prefab : MonoBehaviour { //private GameObject aeroplane_prefab; // Use this for initialization void Start () {
      setInitialPosition();
 }
 // Update is called once per frame
 void Update () {
     float speed =Random.Range(20f,10f);
     transform.Translate(Vector3.back * speed * Time.deltaTime);
     if (gameObject.transform.position.z <= -38)
     {
        //Destroy(gameObject);
       // 
         StartCoroutine(enemySpawn());
         Instantiate(gameObject, transform.position, Quaternion.identity);
     }
 }
 void setInitialPosition(){
     float randomy = Random.Range(36f, 46f);
     float x = 0;
     float y =randomy;
     float z = 45f;
     transform.position = new Vector3(x, y, z);
 }
 IEnumerator enemySpawn()
 {
         //Destroy(gameObject);
         yield return new WaitForSeconds(10f);
         Debug.Log("timeup");//just to check if its working
         }
  }  
               Comment
              
 
               
              Answer by spinaljack · Sep 29, 2010 at 02:07 PM
e.g. instantiate code with a yeild
var prefab : GameObject;
 
               function Start(){ var waitTime : float = Random.Range(0.0,100.0);
  yield WaitForSeconds(waitTime);
  Instantiate(prefab); } 
http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                