- Home /
Error: an object reference is required for a non-static field, method, or property "MouseLook.Enabled"
This is my code. MouseLook is the class that I want to set Enabled from, Enabled is a bool.
if (Input.GetKeyDown (KeyCode.Escape)) {
Paused = !Paused;
if (Paused) {
MouseLook.Enabled = false;
Time.timeScale = 1;
NGUITools.SetActive (PauseMenu, false);
}
if (!Paused) {
MouseLook.Enabled = true;
Time.timeScale = 0;
NGUITools.SetActive (PauseMenu, true);
}
}
I don't think "Enabled" has a capital E as the first letter. Try:
$$anonymous$$ouseLook.enabled = false;
also, how are you declaring "$$anonymous$$ouseLook" at the top of the script?
$$anonymous$$ouseLook is the class. You need to get the specific instance of that class attached to whatever game object has the script (usually used for a camera). I cannot tell from this section of code whether you are writing in C# or Javascript.
$$anonymous$$ouseLook is the class correct, it is attached to my "Player" object
Answer by RafaelCN · Mar 14, 2014 at 07:18 PM
Create an reference or a object reference for the MouseLook. It seems that you don't have this reference, try this:
//set the reference on the inspector
GameObject objectReference;
MouseLook mouse = objectReference.GetComponent<MouseLook>();
mouse.enabled = false;
Ha, did you notice that I already told him this in the comments of his question??
You are right @Lounge$$anonymous$$att :). And @highpockets i just saw you saying "also, how are you declaring "$$anonymous$$ouseLook" at the top of the script?".
If you check the rest of the comments (33 something long, there is a little button there that says 'show 33 more replies'.). We've been working on this for a while.
So sorry @highpockets, yeah, you're right, i didn't saw the button with 33 replies, sorry!
Answer by LoungeKatt · Mar 15, 2014 at 07:19 AM
Make sure the MouseLook script is being compiled before you reference it. To do this, you could edit the script priorities in the Editor, or for simplicity, place it in a folder in Assets, such as Plugins with the script you are accessing it from in the root Assets folder.
Once that is done, you can either set a public GameObject variable to take the reference from, or run a Find on the name or tag of the MouseLook GameObject. This will give you a reference to the main object, which we will call mouseObject.
After you have a reference to the GameObject, use:
MouseLook mouseLook = mouseObject.GetComponent<MouseLook>();
mouseLook.enabled = true;
It the object you are referencing from is the primary camera (has the MainCamera tag) then you can skip over the GameObject location and use Camera.main instead of mouseObject.
Your answer
Follow this Question
Related Questions
error CS0120: An object reference is required to access non-static member `Healthbar.health' 4 Answers
Need Help With XML Reading Class Initialization 1 Answer
An instance of type "x" is required to access non-static variable "y" 2 Answers
Send Log file with email? ( IOException: Sharing violation on path) HELP 0 Answers
Setting the transform of an instantiated game object 1 Answer