Destroy instantiated object instead of prefab

Hello, I’m trying to write a script that gives the user two choices. The user either chooses left or right and then it repeats. However, I’m trying to do the following:

  1. Destroy both objects when the user either presses left or right
  2. Make sure that both choices are not the same (e.g. left and right choices cannot both be healthy food)

So I’m trying to work on the first item and I’m having a lot of trouble. I’m REALLY new to scripting and C# in general so I’m not sure how to destroy the cloned objects instead of the prefab. I had a function for destroying the objects but it just deleted the prefabs instead. After that, I tried having a separate script for destroying objects that would be applied to cloned objects but I’m not sure how to apply them to cloned objects. I could really use some help here on the first part (and if you’re feeling generous the second part).

Here’s my code:

using UnityEngine;
using System.Collections;
        
        public class randomFood : MonoBehaviour {
        	
        	public float randFloat;
        	public GameObject healthyFood;
        	public GameObject unhealthyFood;
        	public GameObject notFood;
        	public Transform emptyObject;
        
        	void Start ()
        	{
        		StartGame ();
        	}
        
        	void Update ()
        	{
        		if (Input.GetKeyDown (KeyCode.LeftArrow)) {
        			print ("Left Choice");
        			StartGame ();
        		} else if (Input.GetKeyDown (KeyCode.RightArrow)) {
        			print ("Right Choice");
        			StartGame ();
        		}
        	}
        
        	void StartGame ()
        	{
        		randFloat = Random.Range (0, 70);
        		if (randFloat <= 40) {
        			Instantiate (healthyFood, emptyObject.position, emptyObject.rotation);
        			Debug.Log ("Healthy Food");
        		}
        		{
        		if (randFloat > 40 && randFloat <= 60) {
        			Instantiate (unhealthyFood, emptyObject.position, emptyObject.rotation);
        			Debug.Log ("Unhealthy Food");
        		}
        		{
        		if (randFloat > 60 && randFloat <= 70) {
        			Instantiate (notFood, emptyObject.position, emptyObject.rotation);
        			Debug.Log ("Not Food");
        				}
        			}
        		}
        	}
        }

And here’s the destroy script:

using UnityEngine;
using System.Collections;

public class DestroyedFood : MonoBehaviour
{
	
	void Start ()
	{
		if (Input.GetKeyDown (KeyCode.LeftArrow)) {
			Destroy (gameObject);
		} else if (Input.GetKeyDown (KeyCode.RightArrow)) {
			Destroy (gameObject);		
		}		
	}
}

Please help me out! Thanks! :slight_smile:

Just save the instance in a variable so you are able to destroy that instance later. And for now you can get rid of your DestroyedFood class, as you will not need that one :slight_smile:

To destroy the instance, add this to your randomFood class:

private GameObject _instance;

Then extend your Instantiate(… calls like this:

_instance = Instantiate(

And before your StartGame (); calls in your Update method just destroy the instance:

Destroy(_instance);

You actually cant destroy prefabs unless you use DestroyImmediate.

I’m guessing you have linked your script referenced objects from objects that actually already exist in scene. This is not a prefab.

Drag these objects to some folder in the project manager.

Thats a prefab. Drag that blue thing back in to the placeholders in your scripts for your objects. Now you are spawning from a prefab.

You can :

GameObject myGo;
myGo = Instantiate (notFood, emptyObject.position, emptyObject.rotation);
myGo.name = "NameDontMatterGonnaDieInAMinute";
Destroy(myGo);//BANG

I know that this post is a bit old but others will go looking for an answer, just like I entered, and this is what happens:

When you are going to assign the GameObject to the public variable from the inspector, a small window opens, and inside it has two small tabs, named: ASSETS and SCENE, and you should keep in mind to select the prefabs from ASSET, and also keep in mind that you must do it from the object that is in the hierarchy and not from your assets folder where the script is. I attach a screen

,I know that this post is a bit old but others will go looking for an answer, just like I entered, and this is what happens:

When you are going to assign the GameObject to the public variable from the inspector, a small window opens, and inside it has two small tabs, named: ASSETS and SCENE, and you should keep in mind to select the prefabs from ASSET, and also keep in mind that you must do it from the object that is in the hierarchy and not from your assets folder where the script is. I attach a print screen:

1 Like