Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jong · Feb 02, 2013 at 02:28 PM · javascriptobjectenabledmember

'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;
     }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image danilonishimura · Feb 02, 2013 at 02:33 PM 1
Share

What is the type of pauseFilter?

avatar image Wolfram · Feb 02, 2013 at 02:45 PM 1
Share

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.

avatar image aldonaletto · Feb 02, 2013 at 02:54 PM 0
Share

@Wolfram is right: #pragma strict doesn't allow undeclared variables and many other bad practices, avoiding lots of annoying errors.

avatar image Wolfram · Feb 02, 2013 at 02:55 PM 0
Share

Also, it might kinda help if you mentioned the line number where the error occurs...

avatar image jong · Feb 02, 2013 at 03:08 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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").

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · Feb 02, 2013 at 03:19 PM 0
Share

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.

avatar image jong · Feb 02, 2013 at 03:22 PM 0
Share

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 ^_^

avatar image Wolfram · Feb 02, 2013 at 03:25 PM 0
Share

See @aldonaletto's first two lines, or any UnityScript tutorial: just add the desired type with a ":".

avatar image jong · Feb 02, 2013 at 03:31 PM 0
Share

I see. Thank you again.

avatar image aldonaletto · Feb 02, 2013 at 03:56 PM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges