- Home /
Detecting a certain object
Ok, so I have a plane that needs to detect when several objects enter it. When all of the objects have passed through it it needs to play a sound, wait a few seconds then load another scene. Say the object it needed to detect had the name "MainCube" what script would I need to do this?
At the moment I have
function OnTriggerEnter (other:Collider)
{
UnityEngine.GameObject = Rigidbody;
if(Collider.name ("MainCube") == true){
Destroy (gameObject);
}
}
Just to test if it will work. But I'm not really any good at coding so that script could just be utter rubbish. So where am I going wrong?
Answer by capnbishop · Feb 13, 2010 at 11:45 PM
I would use tags on the GameObject to identify it, and detect this from the plane. You'll need to add the tag to the GameObject in the Unity editor.
function OnTriggerEnter (other:Collider) { if (other.tag == "SpecialObject") { Destroy(other.transform); }
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("SpecialObject");
if (gos.length == 0) {
// load new scene
}
}
Exactly how this is handled depends on how you want your script to behave. In this example, the object is destroyed when it enters the plane, if it has the tag of the type of object we're looking for. After that, it collects all of the GameObjects with that type of tag. If the array that's returned is empty, then that means that all of these GameObjects have been destroyed, and the script can move on.
Answer by Evil Tebor · Feb 14, 2010 at 12:53 PM
Yeh that was great. Thanks a lot. The only thing was though, I had to change Destroy(other.transform); with Destroy(other.gameObject);
So it now reads.
function OnTriggerEnter (other:Collider) {
if (other.tag == "SpecialObject") {
Destroy(other.gameObject);
}
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("SpecialObject");
if (gos.length == 0) {
// load new scene }}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612065511im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collision Detection for a Prefab? 3 Answers
Collision Detection When Picking Up GameObjects 1 Answer
Collision Checking 1 Answer