Destroying instantiated prefabs [SOLVED]

After 2 days of searching I decided to make My first post.
I’m looking for a clue how I can make following thing:
Here is an example:

  1. Make a simple cube gameobject, rename to a box, add rigidbody, turn off gravity.
  2. Make a script called Destroyer, paste this script and assign to box:
function Test () {
   	Destroy(gameObject);
}
  1. Make new prefab (prefab) and assign box to it.
  2. Delete box from hierarchy view.
  3. Make a new script (Testspawn), paste following script and assign to Main Camera:
var count : int = 0;
var newbox : GameObject;


function Update () {
var newEnemyPosition : Vector3 = Vector3(Random.Range(-10, 10), Random.Range(-5, 5), Random.Range(-3,3));
      var newEnemyRotation : Quaternion = Quaternion.identity;

if (count < 1)
   {


     var newbox : GameObject = Instantiate (newbox, Vector3(9,9,5), newEnemyRotation);
      count++;
   }
     }

5.Make a new script (Testskill), paste following script and assign to Main Camera:

var prefab : GameObject;
function Update ()
{
	if (Input.GetKey ("up")) {
		var other : Destroyer = prefab.GetComponent(Destroyer);
        other.Test();
        }
}

6.Link prefab to newbox and prefab variable in Main Camera inspector view for 2 scripts You just assigned.
7. Play the scene and hit Up arrow on keyboard.
You will notice info: Destroying assets is not permitted to avoid data loss.

Ok. I can agree with that. Destroying this would cause destroying my box in the prefab in my project view. So how I can find another way to destroy it?
The problem is that I can’t drag and drop box gameobject from hierarchy view, becouse it dont exist yet.
I would like to destroy it when i hit up arrow and immediate after that script Testspawn would instantiate new prefab.

How i can tell Unity that I want to destroy GameObject that was just instantiated (which is in Hierarchy View), not mine main prefab?

if (Input.GetKey (“up”)) {
var destroyable : Desroyer= GameObject.Find(“box”).GetComponent(Desroyer);
destroyable.enabled = false;
}

try this :slight_smile:

I tried this and all it can do is to disable script from another script. I was searching for this too for last 2 days…thanks a lot :smile:

Ok. I solved it somehow. I added another blank GameObject variable (newobj) to my spawn script which would be new GameObject and changed:

 var newbox : GameObject = Instantiate (newbox, Vector3(9,9,5), newEnemyRotation);

to

var newobj : GameObject = Instantiate (newbox, Vector3(9,9,5), newEnemyRotation);

so this time new Instantiated gameobject appears in inspector and i can then do what i want with it coz it’s not original prefab.