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.

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.