- Home /
'enabled' is not a member of 'Object'
Help!!
I don't understand what's wrong with it. But the error is there. Or is their another term for enabled in javascript?
function PauseGame(){
savedTimeScale = Time.timeScale;
Time.timeScale = 0;
firstPersonControllerCamera = gameObject.Find("First Person Controller").GetComponent("MouseLook");
mainCamera = gameObject.Find("Main Camera").GetComponent("MouseLook");
firstPersonControllerCamera.enabled = false;
mainCamera.enabled = false;
if (pauseFilter)
{
pauseFilter.enabled = true;
}
}
Please put "`#pragma strict`" as the very first line of every of your scripts it will help you avoid mistakes such as this. There's a reason the editor inserts that line automatically if you create a new script.
@Wolfram is right: #pragma strict doesn't allow undeclared variables and many other bad practices, avoiding lots of annoying errors.
Also, it might kinda help if you mentioned the line number where the error occurs...
the code posted above is just a portion of the entire program. I already put #pragma strict at the first line of my script.
The error occurred on lines 9, 10, and 14 where the word "enabled" is found.
Answer by aldonaletto · Feb 02, 2013 at 02:52 PM
This error message suggests that the variable mainCamera is untyped (Unity assumes Object type in this case), thus the compiler doesn't know what's mainCamera.enabled.
Always declare the variables and their types: you will avoid lots of headaches doing this.
I would do it this way:
private fpsMouseLook: MouseLook; // save the fps MouseLook reference here
private camMouseLook: MouseLook; // save the camera MouseLook reference here
function PauseGame(){
savedTimeScale = Time.timeScale;
Time.timeScale = 0;
// get the fps MouseLook reference only once:
if (!fpsMouseLook) fpsMouseLook = GameObject.FindWithTag("Player").GetComponent(MouseLook);
// get the main camera MouseLook reference only once:
if (!camMouseLook) camMouseLook = Camera.main.GetComponent(MouseLook);
fpsMouseLook.enabled = false;
camMouseLook.enabled = false;
if (pauseFilter){
pauseFilter.enabled = true;
}
}
NOTE: MouseLook is a CS script. If your JS script is in Standard Assets or Plugins (or in one of their subfolders), you may get "MouseLook unknown" errors. You can move your script to a custom Assets subfolder (Assets/Scripts, for instance), or declare the variables fpsMouseLook and camMouseLook as MonoBehaviour and use GetComponent("MouseLook").
Valid advice.
Also note you should always use the script type ins$$anonymous$$d of a string when using GetComponent:
myScriptReference=GetComponent($$anonymous$$yScript); // not GetComponent("$$anonymous$$yScript");
This way, myScriptReference will aready have the correct type.
However, this is not always possible, as with the CS problem @aldonaletto mentions. (At least) in these cases, ake sure you declare your variables already with the correct type.
Sorry I'm still new to unity and javascript. Can you give me a line on the sample reference.
This is how i declared my variables on my script
private var savedTimeScale : float;
private var firstPersonControllerCamera;
private var mainCamera;
private var pauseFilter;
so i'll just have to change it. Thanks ^_^
See @aldonaletto's first two lines, or any UnityScript tutorial: just add the desired type with a ":".
Just change them as follows:
private var firstPersonControllerCamera: $$anonymous$$ouseLook;
private var mainCamera: $$anonymous$$ouseLook;
If pauseFilter is a script reference, declare it as the script name - each script is a class, and its type is derived from the file name. If the script is called PauseFilter.js, declare pauseFilter like this:
private var pauseFilter: PauseFilter;
Notice the way I'm finding the scripts: if wasn't found yet, the variable returns null, thus GetComponent is used. Once it was found, the variable isn't null anymore and there's no need to waste time with the expensive Find or GetComponent.
Your answer
Follow this Question
Related Questions
Moving an object 0 Answers
Saving Object Position in Float NOT Object 1 Answer
A node in a childnode? 1 Answer
Delete last 5 Clones so only 5 exist 2 Answers
Unity 4.6 beta button with Javascript rather than C# 2 Answers