- Home /
Disabling a gui texture through triggers
The following script allows me to enter a trigger, and will display a gui texture. but i also want the gui texture to dissapear when i exit the trigger. how would i do this? i tried destroying but it destroyed the prefab also... can i somehow disable it? script:
function OnTriggerEnter (PlayerCollider : Collider) { if(PlayerCollider.gameObject.name == "player") {
var instance : GUITexture = Instantiate(openDoorText,Vector3(.5, .5, 0), Quaternion.identity);
}
}
Answer by GuyTidhar · Jul 05, 2011 at 07:56 PM
You must have been trying to destroy the prefab and not the instance, therefor you should place a variable outside of the scope of the OnTriggerEnter function so you can access it on other functions and in your case OnTriggerExit:
var guiTexturePrefab : GUITexture;
var doorUp : AnimationClip;
var timer : float = 6.0;
private var instance: GUITexture;
function OnTriggerStay (mytrigger : Collider)
{
if(mytrigger.gameObject.name == "player" )
{
door = gameObject.Find("door");
if(Input.GetButton("openDoor"))
door.animation.Play("hoistDoorUp");
}
}
function OnTriggerEnter (PlayerCollider : Collider)
{
if(PlayerCollider.gameObject.name == "player")
instance= Instantiate(guiTexturePrefab,Vector3(.5, .5, 0), Quaternion.identity);
}
function OnTriggerExit (PlayerCollider : Collider)
{
if(PlayerCollider.gameObject.name == "player" && instance)
Destroy(instance);
}
that works, but when i go back into the trigger, the instance wont come back saying that the object was destroyed. how do i fix that?
Where you actually use the instance, you should check if it exists before using it: if ( instance ) yourFunction();
You are welcome to add that peace of code so I can have a look.
alright im adding it as an answer, just respond there.and i forgot to add the variables i had at the top. heres the whole page of code, just look at the ontrigger functions.
In the code you posted you are trying to instantiate the instance using the instance - you can not do that. I changed the code here: You should drag the prefab of type GUITexture which you created before run-time into the guiTexturePrefab variable; Then this variable is used to instantiate a new GUITexture each time you call instantiate. Your "instance" variable saves the new created GUITexture and then you can use it.
Answer by Kag359six · Jul 05, 2011 at 09:27 PM
var doorUp : AnimationClip; var instance : GUITexture; var timer : float = 6.0;
function OnTriggerStay (mytrigger : Collider) {
if(mytrigger.gameObject.name == "player" ) {
door = gameObject.Find("door");
if(Input.GetButton("openDoor"))
door.animation.Play("hoistDoorUp");
}
}
function OnTriggerEnter (PlayerCollider : Collider) { if(PlayerCollider.gameObject.name == "player") {
instance = Instantiate(instance,Vector3(.5, .5, 0), Quaternion.identity);
}
}
function OnTriggerExit (playerCollider : Collider) { if(playerCollider.gameObject.name == "player" && instance)
Destroy(instance);
}