- Home /
error CS0120 in Unity script
Hi guys, I'm getting this error in unity: Assets/Standard Assets/Scripts/General Scripts/ActivateTrigger.cs(53,52): error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.SetActive(bool)'
This code gives me the error:
break;
case Mode.Activate:
targetGameObject.SetActive(true);
break;
case Mode.Enable:
if (targetBehaviour != null)
targetBehaviour.enabled = true;
break;
case Mode.Animate:
targetGameObject.animation.Play ();
break;
case Mode.Deactivate:
GameObject.SetActive = false;
break;
Answer by Benproductions1 · May 23, 2013 at 11:13 PM
Hello,
When you are trying to access the GameObject your script is attached to, you need to put it in lower case: gameObject.
It's the difference between accessing a recipe and a cake following the recipe (I like cake metaphors)
Another problem is that SetActive is a function, more specifically a method, so instead you have to call it and pass it either true or false, instead of trying to set it to true or false.
Usually, unless your using .NET classes, every variable in Unity is non-capitalised and every function/class is capitalised, so you know something is wrong when your trying to set something thats capitalised :)
Hope this helps, Benproductions1
Answer by robertbu · May 23, 2013 at 11:05 PM
Line 13, game object should be lower case and SetActive() is a method:
gameObject.SetActive(false);
I fixed the error I had thanks to the code robertbu gave me but then a lot more mistakes appeared in different script codes such as: Assets/Editor/CutsceneBirdsEditor.js(11,46): BCE0018: The name 'CutsceneBirds' does not denote a valid type ('not found'). Did you mean 'System.CodeDom.Compiler.CodeCompiler'?
Assets/Editor/CutsceneBirdsEditor.js(20,28): BCE0018: The name 'Seagull' does not denote a valid type ('not found'). Did you mean 'UnityEngine.Social'?
$$anonymous$$y question is why do I even care about this script when it isn't even something I use in my game. It's just in the assets. Can I just delete the scripts that give me an error? I started getting script errors all of a sudden, wasn't even tweaking the scripts as I'm not a coder.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
do you know what version of unity was released in the end of 2012? 0 Answers
Animating a cubemaps orientation 1 Answer
How to import the object from server to unity 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers