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:
- Make a simple cube gameobject, rename to a box, add rigidbody, turn off gravity.
- Make a script called Destroyer, paste this script and assign to box:
function Test () {
Destroy(gameObject);
}
- Make new prefab (prefab) and assign box to it.
- Delete box from hierarchy view.
- 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?