- Home /
gameObject.enabled not working
So i am making a script that allows me to cycle through two cameras when i press tab. There is a first person and third person camera. Which are defined in variables in the script. Ex. my variable is thirdPersonCamera and the line of code is
thirdPersonCamera.enabled = true;
Is there something wrong with this? Because the error message is
MissingFieldException: Field 'UnityEngine.GameObject.enabled' not found.
Any help is appreciated thank you.
but i have two cameras so setting it to a camera ins$$anonymous$$d of a gameObject wouldn't work. Also when i set things to active ins$$anonymous$$d of enabled there is no error, however when i press tab the thirdPersinCamera does not turn on or off same goes for the firstPersonCamera so therefore it doesnt switch cameras
I think you're misunderstanding Camera variable declarations. Technically, a camera IS a GameObject, you're assu$$anonymous$$g that each camera when declared is going to be labeled as the 'main' camera, which is not true. I suggest you read through the Camera part of the Unity Scripting documentation to get a better understanding of how switching between cameras works.
Not quite how I'd explain it. A Camera is not a gameObject, however every camera is attached to a gameObject. The camera is the component that does all the rendering, however the gameObject itself still exists. In any case, it's definitely more appropriate to use a Camera type variable in this case, since we're talking about cameras here, not gameObjects.
Answer by dannyskim · Dec 04, 2011 at 06:12 AM
The error explains it all. There is no 'enabled' variable that is associated with a Game Object. What you're most likely thinking of is 'active' when you are accessing a Game Object directly. So what you're most likely looking for is " thirdPersonCamera.active = true; ". Camera's do have an 'enabled' variable, and the reason why you're most likely not getting access to it ( i'm guessing ) is that you're declaring the camera as a GameObject when it needs to be declared as a Camera in your variable declarations. Otherwise, enabled would work.
I meant to delete my comment from above to re-post as an answer. Don't see harm in copying and pasting my own words if you were insinuating plagiarism.
No, I didn't mean anything bad when I said it. I was for fun.
Answer by Draze · Aug 06, 2015 at 11:10 PM
For any future readers as of Unity 5 gameObject.active = true; is depricated. This is now called SetActive(true)
C#:
gameObject.SetActive(true);
I've just opened an old project here and there are 90 errors like these. I think that ignoring the warnings while developing in Unity 4 was not a good idea :( Thanks for the comment here!
Your answer