How to switch things on and off! basic query

Hi there

Just wondered how you address elements to switch them off in code?

Eg. ive got a gui texture of a key, and I want to switch it off as standard - like as if i were to select it in the hierarcy and then click the tick box next to its name in the inspector to disable it.

I assume its just setting something to false, but i dont know the syntax.

Sorry to ask such a beginner query!

Cheers,

Will

The so called tick is the variable active, documented here.

To clarify: GameObject’s active property controls the whole game object (with all it’s components). Most components can be individually enabled or disabled using enabled property.

Hi guys,

thanks for the tips but I still dont have the hang of addressing the GUITexture to disable it. From the code below you can see i’ve tried switching off the gameObject itself, this only works to deactivate it (see the commented out line in the Start function) and doesnt switch it back øn, so that method is out. Now as you can see im trying to use the “enabled” method again as per your recommendation, but being fairly new to this scripting, I cant seem to interpret what the scripting reference is telling me to do - theres no examples so Im finding it hard going, heres what I have -

static var gotKey : boolean = false;
 

function Start(){

	gotKey = false;
//	GameObject.FindWithTag("KeyGUI").active = false;

GameObject.FindWithTag("KeyGUI").GUITexture.enabled = false;

}

function OnControllerColliderHit (hit : ControllerColliderHit) {
 
	//check and see if the collision has a tag called "key"
	if (hit.collider.gameObject.tag == "key"){
		
		//destroy the collided with object
		Destroy (hit.collider.gameObject);
		
        gotKey = true;
        
        
        //GameObject.FindWithTag("KeyGUI").active = true;
        GameObject.FindWithTag("KeyGUI").GUITexture.enabled = true;
		
		print(gotKey);
 

	}
}

Any Help much appreciated!

Cheers,

Will

To get a component from a game object by class name, you can use the GetComponent() method (also always check the return value from Find* and GetComponent in case it didn’t find anything)

static function EnableTaggedGUITexture(tag : String, enable : boolean) {
   var go = GameObject.FindWithTag(tag);
   if(go) {
      var gt = go.GetComponent(GUITexture);
      if(gt) {
        gt.enabled=enable;
      }
      else {
          Debug.Log("Error: The GameObject does not have a GUITexture component to enable", go);
      }
   }
   else {
      Debug.Log("Error: Could not find a GameObject tagged "+tag);
   }
}

You can paste the above function into your script and then call it like this:

// enable GUIText:
EnableTaggedGUITexture("KeyGUI", true);

// disable GUIText:
EnableTaggedGUITexture("KeyGUI", false);

Edit: Fixed a small error: bool → boolean
Another edit: Fixed a small error: string → String

Freyr, thanks for the reply … but…

I think im missing on something I should be changing, its currently not liking “bool” in line 4, I get what its trying to do but i’ve been unable to edit it to fix it, not sure what I need to change…

static var gotKey : boolean = false;
 
 
static function EnableTaggedGUITexture(tag : string, enable : bool) { 
   var go = GameObject.FindWithTag(tag); 
   if(go) { 
      var gt = go.GetComponent(GUITexture); 
      if(gt) { 
        gt.enabled=enable; 
      } 
      else { 
          Debug.Log("Error: The GameObject does not have a GUITexture component to enable", go); 
      } 
   } 
   else { 
      Debug.Log("Error: Could not find a GameObject tagged "+tag); 
   } 
} 

function Start(){

	gotKey = false;

		EnableTaggedGUITexture("KeyGUI", false);

}



function OnControllerColliderHit (hit : ControllerColliderHit) {
 
	//check and see if the collision has a tag called "key"
	if (hit.collider.gameObject.tag == "key"){
		
		//destroy the collided with object
		Destroy (hit.collider.gameObject);
		
        gotKey = true;
		
		EnableTaggedGUITexture("KeyGUI", true);
		
		print(gotKey);
 

	}
}

Ah… I guess that’s what I get when I don’t test my code before posting it. (I don’t have my Mac nearby, so I was not able to.) That “bool” on line 4 should have been “boolean”.

No I thought that, if i change that to boolean, it tells me “string” isnt valid…

Ah… that should have been “String”. (With an uppercase “S”) … I’m not as sharp on the Javascript built-in type names as I thought – should just have left them out completely.

Now ive sorted those 2 out (capital S and boolean) i am getting a null reference exception…

UnityEngine.GameObject.set_active (Boolean value)
keyPickup.js.OnControllerColliderHit (UnityEngine.ControllerColliderHit hit) (at Assets/keyPickup.js:20)

which is odd, as line 20 is the beginning of function Start(){

thoughts?

Try opening the error console and see if there are other compiler errors before that error.

It looks like you are getting an error from a previous version of the script. (The Null reference is thrown when trying to set active on a GameObject, and that line has been removed in the latest version.)

Yes you are indeed correct, the script is now doing what its intended to, I will look over it again and make sure I understand how its doing it, if not may ask you exactly whats going on, but i think i understand it, thanks for your time :slight_smile:

It’s simple really.

It firsts calls GameObject.FindWithTag() to locate the game object, then it calls GetComponent() to get a reference to its GUITexture. Then it assigns the value of ‘enable’ (Which is either true or false) to the ‘enabled’ property of the gui texture.

All the saving into temporary variables and if()s etc. are just so it can do error checking. (FindWithTag and GetComponent both return null if they can’t locate what you are looking for.