Instantiating a new cube once the old one is destroyed C#

Hi

I was trying instantiate a new cube once the ols cube is destroyed - this sounded somewhat easy to do, but wasn’t !

using UnityEngine;
using System.Collections;

public class RotatingCube : MonoBehaviour {
	
	public float cubeSpeed = 50.0f;
	
	public float cubeRotationSpeed = 250.0f;
	
	public bool isCubeActivated = false;
	
	public Transform RotatingCubePrefab;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if(isCubeActivated == true){
		
			transform.Rotate(new Vector3(0,1,0) * cubeRotationSpeed * Time.deltaTime);
		
			//transform.Translate(new Vector3(0,0,1) * cubeSpeed * Time.deltaTime); //using this code the cube will go around in a circle
		
			transform.Translate(new Vector3(0,0,1) * cubeSpeed * Time.deltaTime,Space.World); //using this code instead will make the cube go along the z-axis while spinning
			
			gameObject.transform.renderer.material.color = Color.cyan; //this line will give the cube a color once the user click on it
			
			Destroy(gameObject,2.0f); //Destroy the gameObject after 2 sec.
		}
		
		if(Destroy(gameObject,2.0f) == true){
			
			Instantiate(RotatingCubePrefab,transform.position,transform.rotation); //instantiates a new cube once the old one is destroyed
		}
	}
	
	void OnMouseDown (){
		
		isCubeActivated = true; //the bool: isCubeActivated become true once the mouse pointer clicks on the cube
		
	}
	
}

I knew that I wanted to a new cube once the old one was destroyed so I tried:

if(Destroy(gameObject,2.0f) == true){
			
			Instantiate(RotatingCubePrefab,transform.position,transform.rotation); //instantiates a new cube once the old one is destroyed
		}

I’m quite sure that the condition in the if-statement is wrong, but I’m not sure what I should call it

The error message I get is:

Assets/Scripts/RotatingCube.cs(37,20): error CS0019: Operator ==' cannot be applied to operands of type void’ and `bool’

You can’t do anything once the cube is destroyed. You need to instaniate and then destroy the cube.

Instead of Destroy(gameObject,2.0f); //Destroy the gameObject after 2 sec. and

if(Destroy(gameObject,2.0f) == true){

Instantiate(RotatingCubePrefab,transform.position,transform.rotation); //instantiates a new cube once the old one is destroyed

}

you could do

void DestroyAfterTime(){
Instantiate(RotatingCubePrefab,transform.position,transform.rotation); //instantiates a new cube once the old one is destroyed
Destroy(gameObject);
}

and place this line:

Invoke("DestroyAfterTime", 2f);

instead of these:

      Destroy(gameObject,2.0f); //Destroy the gameObject after 2 sec.

        }

        

        if(Destroy(gameObject,2.0f) == true){

            

            Instantiate(RotatingCubePrefab,transform.position,transform.rotation); //instantiates a new cube once the old one is destroyed

        }

Something like

#region Imports

using UnityEngine;

#endregion

public class RotatingCube : MonoBehaviour
{
    #region Public Fields

    public Transform RotatingCubePrefab;
    public float cubeRotationSpeed = 250.0f;
    public float cubeSpeed = 50.0f;


    public bool isCubeActivated = false;
    private bool isDying = false;

    #endregion

    #region Private Methods

    private void OnMouseDown()
    {
        isCubeActivated = true; //the bool: isCubeActivated become true once the mouse pointer clicks on the cube
    }


    // Use this for initialization

    private void Start()
    {
    }


    // Update is called once per frame

    private void Update()
    {
        if (isCubeActivated  !isDying)
        {
            transform.Rotate(new Vector3(0, 1, 0)*cubeRotationSpeed*Time.deltaTime);


            //transform.Translate(new Vector3(0,0,1) * cubeSpeed * Time.deltaTime); //using this code the cube will go around in a circle


            transform.Translate(new Vector3(0, 0, 1)*cubeSpeed*Time.deltaTime, Space.World); //using this code instead will make the cube go along the z-axis while spinning


            gameObject.transform.renderer.material.color = Color.cyan; //this line will give the cube a color once the user click on it
            isDying = true;Invoke("DestroyAfterTime",2f);
        }
    }

    void DestroAfterTime()
    {
            Instantiate(RotatingCubePrefab, transform.position, transform.rotation); //instantiates a new cube once the old one is destroyed
            Destroy(gameObject);
    }
    #endregion
}

Basically what Elite said. You’re destroying your script with the cube because you’ve attached the script on the cube.

Hi

@EliteMossy

Thank you for taking your time to re-write my script - I’ll guess I could just copy and paste it into my original script and then it would probably work just fine, however, then I won’t learn much scripting from doing so.

But I do find it beneficial to study the changes you have made in order to learn from it.

But even though I destroy the cube and the script attached to it, then I should be able to get a new one by instantiating a new one from the prefab, that’s the impression I have - however, I could be wrong, because if the script is destroyed along with the cube, then it would never get to and fire the if-statement where I want the instantiation to happen - please correct me if I’m wrong !

I have looking at an old script from another project I have and from what I can see the only script I have attached to my projectile is a script that destroy the projectile. I have watched the official tutorial on instantiate and in it, it is shown how to create an empty gameobject where all the behaviors of the object is stored and the object itself only have a “Kill-self”, so it’s getting destroyed after a set amount of time.

So, I’m thinking of doing something similar.

Yes when you instantiate an object, if the prefab has a script attached, it will contain a new script as a component. However, prefabs cannot have game objects found within the scene assigned in the inspector. You can only assign other prefabs to a prefab script. This requires some extra work if you have a script that instantiates with a gameobject, such as enemy you want to target the player.