- Home /
gameObject.enabled is not working
im trying to make a GUI text disable and enable to tell the player it can get in to a vehicle, for this im using the next code:
#pragma strict
var seat : Transform;
var isInside : boolean = false;
var enterText = GameObject;
function Start () {
}
function Update () {
}
function OnTriggerEnter (hit : Collider){
if (hit.transform.name == "Player" && isInside == true){
}
enterText.active = false;
}
function OnTriggerExit (hit : Collider) {
if (hit.transform.name == "Player" && isInside == false){
}
enterText.active = true;
}
Comment
if you know any other way of doing it pleas put it i have tried like 10 different ways
Best Answer
Answer by clunk47 · Dec 28, 2012 at 02:12 AM
You were using active instead of enabled. You were defining the text as a gameObject instead of GUIText. You had your true and false statements AFTER your IF statements were closed, rather than having them INSIDE the IF statements. You also were not toggling isInside on or off, so it will always be false. Try this.
#pragma strict
var seat : Transform;
var isInside : boolean = false;
var enterText : GUIText;
function Start ()
{
enterText = GetComponent(GUIText);
enterText.enabled = false;
}
function Update ()
{
if(isInside)
enterText.enabled = true;
else if(!isInside)
enterText.enabled = false;
}
function OnTriggerEnter (hit : Collider)
{
if (hit.transform.name == "Player" && isInside)
{
isInside = false;
}
}
function OnTriggerExit (hit : Collider)
{
if (hit.transform.name == "Player" && !isInside)
{
isInside = true;
}
}
thanks dude i was about to throw my computer out the window...