Show/Hide objects on anyKeyDown

I have 2 planes right in front of my camera, tagged phone". When you press any button (I wanted to do it on Space, but it kept giving me an error about KeyDown not found in UnityEngine or something), those 2 planes are supposed to appear. When you press it again, they’re supposed to disappear. This is my code so far.


function Start(){
	var keypress:boolean=false;
	var phonestuff:Array=GameObject.FindGameObjectsWithTag("phone");
}

function Update () {
	if ((Input.anyKeyDown) && (keypress==false))
	{
		for (any in phonestuff)
		{
			any.enabled=true;
		}
		keypress=true;
	}
	else if ((Input.anyKeyDown) && (keypress==true))
	{
		for (any in phonestuff)
		{
			any.enabled=false;
		}
		keypress=false;
	}
}

However, it keeps giving me an error “enabled is not a member of Object”. My guess is that I should add more stuff to access the actual GameObjects, but when I press . the hints are totally irrelevant (ie any.XXXX.enabled=true;)

What should I do?

(Bearing in mind I program in C# and not JS) Might it be better do do something like this?

var plane1:GameObject;
var plane2:GameObject;
var arePlanesHidden:boolean = false;

function Update(){

	if(Input.GetKeyDown(KeyCode.Space) && arePlanesHidden == false){

		
		arePlanesHidden = true;
		print("Planes Hidden");
		}
		else{
		
	if(Input.GetKeyDown(KeyCode.Space) && arePlanesHidden == true){


		arePlanesHidden = false;
		print("Planes unHidden");
		
		}
		}
		
	if(arePlanesHidden == false){
	
		plane1.SetActive(true);
		plane2.SetActive(true);
		
	}
	
	if(arePlanesHidden == true){
	
		plane1.SetActive(false);
		plane2.SetActive(false);
		
	}
	}

I have tried it and it does work for me. :slight_smile:

Hi,
if you just want to disable the object and not the colliders and others like scripts use this

 var plane1:GameObject;
    var plane2:GameObject;

function Start(){
plane1.GetComponent(MeshRenderer).enabled  = false;
plane2.GetComponent(MeshRenderer).enabled = true;
}
function Update(){
if(Input.GetKeyDown(KeyCode.Space)){
plane1.GetComponent(MeshRenderer).enabled = !plane1.GetComponent(MeshRenderer).enabled;
plane2.GetComponent(MeshRenderer).enabled  = !plane1.GetComponent(MeshRenderer).enabled; 
}
}

Use this for compltly disabling an object

var plane1:GameObject;
var plane2:GameObject;

function Start(){
plane1.active = false;
plane2.active = true;
}

function Update(){

if(Input.GetKeyDown(KeyCode.Space)){
plane1.active = !plane1.active;
plane2.active = !plane2.active;
}
}