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)
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.
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.
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.
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.
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
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.
Update is called every frame. If your game runs at 60 fps, it’s going to execute GeyKey 60 times per second 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?