- Home /
Find and destroy object
I am working on a script that needs to generate some file cabinets with a number of components, such as labels, locks, and files inside. They each use a prefab (filecabinet1, 2, and 3) and have the locks, etc. as children. The way I was planning to have the "randomness" generated was to randomly destroy some of the components on start. However, right now I can not even get a specific object to be destroyed on start. Here is my very simple code where I am trying to do this:
public class SafeSpawn : MonoBehaviour {
void Start() {
GameObject lock1 = GameObject.Find("/File_Cabinet_Drawer1/compliant_lock");
GameObject lock3 = GameObject.Find("/File_Cabinet_Drawer3/compliant_lock");
Destroy(lock1);
Destroy(lock3);
}
void Update() {
}
}125
I do not know exactly why this isn't working. I have tried using a variety of methods (using tags to find objects, but I do not fully understand how to give an object a tag; filling GameObject arrays with items and iterating through with a loop and destroying them) but none have worked. Can someone maybe help tell me why this code isn't working? And maybe give some advice on how may be best to go about this, as there are many components (4 per cabinet, 3 cabinets).
Thanks!!
If you search for "/something/somechild/somedeepchild" then the something has to be in the root of your hierarchy. Are File_Cabinet_Drawer1 and 3 childed to anything? Is so, remove the first /.
Answer by aldonaletto · Jul 02, 2011 at 02:50 AM
I think the safer (but no the wiser) way to do this is creating an array of Game Objects - let's call it details - in the File Cabinet script (you may assign the same script to the three cabinets). Select one cabinet, set the details size and drag the itens you may want to destroy to its empty slots.
using UnityEngine;
using System.Collections;
public class CabinetScript : MonoBehaviour {
public GameObject[] details;
public int qDestroy = 3; // how many itens to destroy
void Start() {
int i;
int n = qDestroy;
while (n>0){
i = Random.Range(0,details.Length);
if (details[i] != null){
Destroy(details[i]);
details[i] = null;
n--;
}
}
}
}
answer to my question actually turned out to be the comment on my question, which was that I was being stupid and forgot i had an additional level on my hierarchy above the file cabinets. But your method definitely helps me figure out how I'm gonna make the actual script work! thanks!
Answer by GuyTidhar · Jul 01, 2011 at 07:58 PM
It might be that the game objects you are looking for has not been created yet. You can try and wait a frame for them to be ready and then look for them. Do that using a coroutine:
public class SafeSpawn : MonoBehaviour {
void Start()
{
StartCoroutine(KillAtStart());
}
private IEnumerator KillAtStart()
{
// Wait a frame before looking for the game objects
yield return null;
GameObject lock1 = GameObject.Find("/File_Cabinet_Drawer1/compliant_lock");
GameObject lock3 = GameObject.Find("/File_Cabinet_Drawer3/compliant_lock");
if ( lock1 != null )
Destroy(lock1);
else
Debug.LogError("Failed to find lock1");
if ( lock3 != null )
Destroy(lock3);
else
Debug.LogError("Wholly crap! could not find lock3 either!");
}