- Home /
Destroy a specific object on trigger?
Hi,
I've got a JavaScript and I'm trying to make a specific object (being a GUITexture) be deleted once the Player passes a certain point, which is marked by another object. The JavaScript inside this object is:
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "player"){
Destroy (gameObject);
}
}
With intentions to destroy the 'GUIText' component when it is triggered by the player. Unfortunately, when testing this, the only object that was deleted was the trigger itself, and I'm not sure how to change this to make it so the 'GUIText' can be deleted on the trigger.
The GUIText should be loaded with the level, but destroyed when a different object is triggered by the player. I'm still learning JavaScript, so if anyone could help me it would be amazing. Thanks much.
Answer by Professor Snake · Feb 18, 2013 at 02:16 PM
gameObject refers to the gameObject that contains the script, which in this case is the trigger. You need to store a reference to the GUIText gameobject somewhere and destroy it via that. For instance, you can either use
var guiTextObject:GameObject;
and assign the gui text object into this variable via the inspector, and then destroy it via
Destroy (guiTextObject);
Or you can use a function like transform.Find() and get the object that way.
Your answer
Follow this Question
Related Questions
Is there anyway to make an object impenetrable? 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How do I call an on trigger enter / destroy gameObject in the scene c# 1 Answer
Why won't the coin destroy when hit? 0 Answers
How come when the player runs into the coin it won't destroy? 1 Answer