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
}
}