My code creates an sphere object after 10 seconds and then its supposed to destroy it after 20 seconds. I see it being spawned after 10 seconds but it just keeps creating Sphere(clones) after 20 seconds. I was hoping that it would spawn after 10 secs and then destroy it after 10 secs and keep repeating the cycle.
Here is the code:
using UnityEngine;
using System.Collections;
public class Spawn_Sphere : MonoBehaviour {
public GameObject Sphere;
public float timer = 0.0f;
public int spawned = 0;
void SpawnIt(){
//Vector3 position = new Vector3(Random.Range(-10.0F, 10.0F), 0, Random.Range(-10.0F, 10.0F));
Vector3 position = new Vector3(350,10,283);
Instantiate(Sphere, position, Quaternion.identity);
spawned = 1;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if (timer > 10 && spawned == 0) {
SpawnIt();
}
if (timer > 20 ) {
Destroy(Sphere);
timer = 0.0f;
spawned = 0;
}
}
}