delay and variable problem

Hello, I am new in unity3D, can somebody explain me why my program doesn’t destroy the clone object and how instantiate an other new object automatically every 5 second??

using UnityEngine;
using System.Collections;

public class cubelife : MonoBehaviour {
    public GameObject des;
    public Transform pref;
    public float t;
    GameObject clone;
	// Use this for initialization
	void Start () {
		Destroy(des,t);
		clone = Instantiate(pref, new Vector3(0,5,0),Quaternion.identity) as GameObject;
		}
	// Update is called once per frame
	void	Update () {
		if(Input.GetButtonDown("Fire1")){
			Destroy(clone,t);
			clone = Instantiate(pref, new Vector3(2,5,0),Quaternion.identity) as GameObject;
		}
		if(Input.GetButtonDown("Fire2")){
			Destroy(clone,t);
			clone = Instantiate(pref, new Vector3(-2,5,0),Quaternion.identity) as GameObject;
		}
	}
}

First, to use WaitForSeconds for delay, your method needs to be an IEnumerator, and it needs to be called from StartCoroutine. You should read up on these topics:

MonoBehaviour.StartCoroutine

Essentially, just make your method “IEnumerator” instead of “void”, use “WaitForSeconds” inside of it, and call it with “StartCoroutine”, and you’ll be golden. However, for this instance, we don’t need it.

Next: when you Destroy an object, it and anything attached to it no longer exist. You Destroy clone, and then in the next line, you attempt to Instantiate it – you can’t do that. Once it’s destroyed, it’s destroyed. Instantiate is basically a copy and paste routine – you can’t copy it if it doesn’t exist, and if you called Destroy on it, it doesn’t exist. Also, when you Instantiate, you are placing a Transform in the first parameter of the method, which means you’re just creating a new Transform, not the entire GameObject. I don’t think this is what you want. Read up on Destroy() and Instantiate().

Object.Destroy

Object.Instantiate

It’s important to understand the fundamentals of these methods and what ramifications they will have, as they are basic Unity3D methods that you will use over and over again to solve common problems. Understanding them will ensure that when you run into a problem, you know what tools to use to create a solution without nasty side effects or unwanted behaviour (like you’re getting now).

Anyway, after reading up, we know that we cannot Destroy an object and then Instantiate it. So, let’s keep a GameObject around in order to clone it. As far as automatically instantiating an object every five seconds, we can use the widely used “incrementing time via Time.deltaTime” method for that. Again, read up:

Time.deltaTime

Anyway, the following script will create the correct object when you click the fire buttons, and automatically create objects every five seconds below the two that you are creating manually. Please read the above portions of the documentation and note the comments in the script to truly understand what you are doing.

using UnityEngine;
using System.Collections;

public class cubelife : MonoBehaviour
{
	// Attach this script to an object (such as the camera) in your scene.
	
    public GameObject originalObject;			// Object that will be cloned. Drop a GameObject here in the Inspector.
	private GameObject clone;					// Manually-created clone.
	private GameObject autoObject;				// Automatically created clone.
	private int numberOfCreatedObjects = 0;		// Number of times the script has created a clone automatically.
    private float instantiateTimer = 0;			// Timer for automatically creating clones.
	private float t = 0;						// Time to wait before destroying an object.
	
    void Start ()
	{
		// When we start, we immediately clone the original object.
		clone = Instantiate (originalObject, new Vector3 (0, 5, 0), Quaternion.identity) as GameObject;
    }

    void Update ()
	{
		// To create a simple timer, increment a float value by Time.deltaTime each frame.
		instantiateTimer += Time.deltaTime;
		
		// When the timer reaches 5 seconds, do what you want it to do, and then reset it.
		if (instantiateTimer >= 5.0f)
		{
			// Keep track of how many times you have created an object.
			numberOfCreatedObjects++;
			
			// Use the value for the number of objects to offset each one on the x-axis.
			autoObject = Instantiate (originalObject, new Vector3 (numberOfCreatedObjects * 2, 3, 0), Quaternion.identity) as GameObject;
			
			// Reset your timer.
			instantiateTimer = 0;
		}
		
		// This part is basically unchanged, except that we're passing in the original object, not just a transform.
       if(Input.GetButtonDown("Fire1"))
		{
         Destroy(clone,t);
         clone = Instantiate(originalObject, new Vector3(2,5,0),Quaternion.identity) as GameObject;
       }
       if(Input.GetButtonDown("Fire2")){
         Destroy(clone,t);
         clone = Instantiate(originalObject, new Vector3(-2,5,0),Quaternion.identity) as GameObject;
       }
    }
}

There are many ways to achieve what you’re trying to achieve; many are better, but this is about as simple and straight-forward as it gets.

Ok I got it working. I guess I needed to try.

public class NewBehaviourScript : MonoBehaviour {
    public GameObject clone;
    public Transform pref;
    void Start(){
        clone = Instantiate(pref, new Vector3(0, 5, 0), Quaternion.identity) as GameObject;
    }
    void Update(){
        if (Input.GetButtonDown("Fire1")){
            var temp = clone;
            Destroy(temp,2f);
            clone =  Instantiate(pref, new Vector3(2, 5, 0), Quaternion.identity) as GameObject;
        }else if (Input.GetButtonDown("Fire2")){
            var temp = clone;
            Destroy(temp,2f);
            clone =  Instantiate(pref, new Vector3(-2, 5, 0), Quaternion.identity) as GameObject;
        }
    }
}

The issue is you need to place your reference to an object inside another reference -temp- and destroy this one.