Enable / Disable GameObjects

I’ve been trying to use a script located on my camera to disable another GameObject in the scene…

Sadly I haven’t been able to figure it out.
Code is in JavaScript (not sure how to change it in BBcode)

var menuObject : GameObject;
function Update()
{
	if(Input.GetKey(KeyCode.Escape))
	{
		menuObject = GetComponent(GameObject);
		// Check current status
		
			if(menuObject.active = true)
				menuObject.SetActive(false);		
			if(menuObject.active = false)
				menuObject.SetActive(true);
		
		
	}
}

I haven’t done JS in a while but should this be == instead?

I get the following error/warnings after adding ==:
Error:
MissingComponentException: There is no ‘GameObject’ attached to the “CentralPivot” game object, but a script is trying to access it.
You probably need to add a GameObject to the game object “CentralPivot”. Or your script needs to check if the component is attached before using it.

Warning:
GetComponent requires that the requested component ‘GameObject’ derives from MonoBehaviour or Component or is an interface.
UnityEngine.Component:GetComponent(Type)

Not sure what either really means.

I should add: this is odd because I do have a GameObject variable called menuObject on the script that CentralPivot is attached to. And I assigned the GameObject “game_menu” to it.

Am I perhaps using GetComponent incorrectly?

You need to get the menuObject’s (GameObject) transform, and from there set the active/inactive state.

menuObject.transform.SetActive(false);

Yup :slight_smile:

If you want the thing this script is attached to then just use gameObject. You also have issues with your conditionals. = is assignment while == is equality comparison. Also, because you used two if statements they would each evaluate so if this code was written without syntax errors it would turn off and then on your GameObject in the same frame. Use an else instead.

Setup:

So I think I may have figured out the problem. I just don’t know the solution.
When I hit Escape the first time, it is removing the GameObject from the menuObject variable… it just disappears.

Updated Code:

if(Input.GetKey(KeyCode.Escape))
    {
        menuObject = GetComponent(GameObject);
        // Check current status
       
            if(menuObject.transform.active == true)
                menuObject.transform.active = false;       
            else if(menuObject.transform.active == false)
                menuObject.transform.active = true;
       
       
    }

Look at line 7 of your code. You are using GetComponent incorrectly, unless I am missing something (don’t use javascript). You are calling GetComponent which returns only components, which are not GameObjects. If you did assign menu object in the inspector, line 7 is entirely unnecessary.

EDIT: looking at first post

Just remember to avoid using menuObject.transform as much as you can.
The best practice is to Cache this transform like:

private Transform menuObject;
void Start()
{
  menuObject = this.transform;
}

It doesn’t disappear - it gets set to null because GetComponent didn’t find anything (because GetComponent doesn’t work with GameObject as an argument). As others have said, if you are assigning the value via the Inspector then that line is completely unnecessary. You can also combine that entire if-else into a single line

menuObject.SetActive(!menuObject.active);

It seems I get rid of the errors with everyone’s help. Modified my code with suggestions from everyone.
New code setup:

var menuObject : GameObject;
function Start()
{
     menuObject.SetActive(false);
}
function Update()
{
if(Input.GetKey(KeyCode.Escape))
{
     menuObject.SetActive(!menuObject.active);
}
}

Issue I’m running into now is that the objects are flashing. I believe this is because the function is called during a Update, which I means it’s being called multiple times ?

It’s because you’re setting it to the opposite of what it is. When active is true, !active is false. When active is false, !active is true. If you only want to disable it, use false. If you do want to toggle it, change your GetKey to GetKeyDown and it will execute once per keystroke.

As it is now with getkey, !active and running in update… your code is executing every frame while that key is held down and it’s set to the opposite of the current active state.

Perfect! The answer was the GetKeyDown. I’m guessing the GetKey will grab whatever key you’re pressing for the entire duration of the update?

Update is called every frame. If your game runs at 60 fps, it’s going to execute GeyKey 60 times per second :wink: The flickering was you enabling and disabling the object 60 times haha

This won’t help your flashing but on a related side note, .active has been removed in Unity 5. In Unity 4 it’s deprecated, so it works but they don’t want you to use it. But in Unity 5 you’ll get an error and it wont work at all. (i had to change like 191 instances in my code when i upgraded).

So i would start using .activeSelf instead now so you dont run into problems later

In your code it would be menuObject.SetActive(!menuObject.activeSelf);

I wonder what convention they used for that change. Instead of isActive they went to activeSelf. Maybe it had something to do with parent and child gameobjects?