- Home /
Two objects are placed at the same place. How to destroy one and keep another?
I am sorry to ask a question without even a draft of a script, but...
If I have two prefabs placed roughly at the same point, so parts of them intersect, how can I always destroy object a and keep object b? Probably I should use the collisions or something like that, but I just can't figure out the exact algorithm. It is not a bug, but a free generating space, so I have problems controlling what appears where, so I am trying to find/make a script to prevent these cases.
This is the script I use to spawn objects. I just use it again, changing the z value to have +10 always.
var Xvalue:float = 0.0;
var Zvalue:float = 0.0;
var ChangeCounter:int = 1.0;
var ChangeAmount:float = 20.0;
var Technical_Couter:int = 1.0;
var PlayFieldSize:float = 0.0;
var prefab1:Transform;
function Update ()
{
if(ChangeCounter<PlayFieldSize)
{
Xvalue=(ChangeAmount*ChangeCounter);
Instantiate (prefab1, Vector3(Xvalue, 0.0, 0.0), Quaternion.identity);
if(ChangeCounter>0)
{
while(Technical_Couter<(ChangeCounter+1))
{
Zvalue=(ChangeAmount*Technical_Couter);
Instantiate (prefab1, Vector3(Xvalue, 0.0, Zvalue), Quaternion.identity);
Technical_Couter++;
}
while(Xvalue>0)
{
Xvalue=(Xvalue-ChangeAmount);
Instantiate (prefab1, Vector3(Xvalue, 0.0, Zvalue), Quaternion.identity);
}
}
ChangeCounter++;
Technical_Couter=1.0;
}
}
Also, the spawned prefab in its turn spawns one of a few prefabs at its place.
So I need to check, if the prefab at the bottom layer is too high, and collides with a prefab at the top layer, and if so, destroy the one on the top.
Answer by DaveA · Oct 26, 2012 at 08:26 PM
I'm guessing you generate objects by doing something like this:
Instantiate (someObject, somePosition, someRotation);
and then don't keep track of that new object.
Keep track of it.
You could use a List for example:
var objList : List = new List();
var newGO = Intantiate (someObject....
objList.Push (newGO);
or something like that. Then you can look for objects using the objList list.
So, would it be possible to check if the prefab at the bottom layer is too high, and collides with the one on top, and destroy the top one using this list?
Your answer
![](https://koobas.hobune.stream/wayback/20220613082924im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destroy Turret with machine Gun 0 Answers
Destroy object when hit by other object 1 Answer
How to Destroy a gameobject on collision 3 Answers
[SIMPLE] Not loose speed when destroying colliding object 1 Answer
Destroy object after 5 collisions 1 Answer