Hi,
I’ve got a particular problem I’m trying to solve and I wondered whether any of you guys could help point me in the right direction.
I have a script that spawns 20 instances of the same prefab. The Prefab has a script attached with instructions on what should happen if the player selects a prefab with the mouse. That side of things is relatively simple.
However, I want to add something to the script that will ensure that when a prefab is selected with the mouse, something happens to the non selected prefabs.
For example, imagine I have 20 boxes. If the player selects box 1, I want to destroy boxes 2-20.
Does anyone have any idea how I’d go about doing this?
Thanks in advance.
private var selected : boolean;
function Update(){
if(selected){
var boxes = FindObjectsOfType(MyBoxScript);
for (var box : MyBoxScript in boxes){
if(box.gameObject != this.gameObject){
Destroy(box.gameObject);
}
}
}
}
function OnMouseDown () {
selected = true;
}
Not tested, but probably similar to what you’re looking for. Ultimately you’re going to need to use FindObjectsOfType or FindObjectsWithTag somewhere, though, that was my point 
Thanks for the tip. Unfortunately I should have mentioned earlier that I’m working with C#. Your tip regarding the “FindObjectsOfType” command got me digging around and I’ve ended up with this:
GameObject[]boxes = GameObject.FindGameObjectsWithTag("LevelButton");
foreach (GameObject box in boxes)
{
if (box.gameObject != this.gameObject)
{
Destroy(box);
}
}
Unfortunately, this destroys all of the boxes as soon as I click on one.
Can anyone help?
It’s also destroying the object that is running this code?
Yes.
The 20 boxes are all instantiated from the same prefab. When a box is clicked, all 20 are destroyed.
What object is the script that contains the code you posted attached to, though?
Sorry.
The script quoted above is attached to the same prefab.
So this script is on every box, correct? Or the object instantiating the boxes?
legend, it is on every box…
Id use a list and add each spawned prefab to the list while also passing it a unique id
The you can just loop through the list and kill all the prefabs except the one with your id.
Thi s does assume that the creation function and the kill function are stored on a different object of course. If you really wanted the actual boxes to be able to kill themselves you could jsut compare the transform locations of each one to the location of the calling one and use that to elimiate it from the kill.
something like
public List<GameObject> m_myList = new List<GameObject>();
////blah blah blah then your function to spawn the boxes
GameObject go;
for(int i=0;i<20;i++)
{
go=Instantiate(P_Prefab,spawnpos,Quaternion.identity) as GameObject;
go.GetComponent<C_BoxScript>().m_Identity=i;
m_myList.Add(go);//add to my list
}
//blah blah blah function to kill
void KillboxesExceptFor(int ID)
{
for(int i=0;i<m_myList.Count;i++)
{
if(m_myList[i].GetComponent<C_BoxScript>().m_Identity!=ID)GameObject.Destroy(m_myList[i]);
}
}
or something like that