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 Misto · Oct 11, 2011 at 03:22 AM · mouselook

Mouse Look Help

I'm trying to create a pause menu for my game, and I have most things working correctly, except the camera still looks around with the mouse movement. I searched on here and found something about using

GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = true;

However, it says in the console that "error CS1061: Type UnityEngine.Component' does not contain a definition for enabled' and no extension method enabled' of type UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)" I'm completely lost since nothing seems to be working that I find online.

Any help would be appreciated, if you need to see my entire code I can post it, but this is the only error I'm getting. Thanks.

Comment
Add comment
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

3 Replies

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

Answer by Bunny83 · Oct 11, 2011 at 05:26 PM

I can't even count how often something like this has been asked :D

Well that's why i don't think that UnityScript is the best choice for beginners. It looks simpler but in the end you stump into the same problems.

GetComponent() always returns a reference of type Component. Even the "typeof" version will only return a Component because the compiler can't tell what object will be returned by this fucntion. Only the generic version will return the "correct" type.

There are several ways to solve this.

  1. Use the generic version of GetComponent.<MouseLook>() this will return a reference of the type you've given.

  2. Cast it implicit into the correct type by assigning the returned reference to a variable of the correct type.

  3. Case it explicitly with the as-operator.

Here are some examples:

 //UnityScript
 
 // 1.
 Camera.main.GetComponent.<MouseLook>().enabled = true;
 
 // 2.
 var mouseLookScript : MouseLook;
 mouseLookScript = Camera.main.GetComponent(MouseLook);
 mouseLookScript.enabled = true;
 
 // 3.
 (Camera.main.GetComponent(MouseLook) as MouseLook).enabled = true;

The first and the third ways is actually the same but the generic version is shorter to write ;)

However usually the best way (best performance) would be to do it the second way and cache the MouseLook script at start:

 var mouseLookScript : MouseLook;
 
 function Start()
 {
     // cache the reference at start
     mouseLookScript = Camera.main.GetComponent.<MouseLook>();
 }
 
 function Update()
 {
    // ...
    mouseLookScript.enabled = true;
    // ...
 }
Comment
Add comment · Show 5 · 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 Misto · Oct 11, 2011 at 07:38 PM 0
Share

That did it Bunny. In my script I was doing the same thing as you did at the end (where you get the component in the start function), just didn't know about the generic version of GetComponent. Thanks.

avatar image darix989 · Feb 20, 2014 at 10:54 PM 0
Share

hi guys, I have the same issue and I'm using the second solution proposed. It gives me this error :

Assets/SwitchFTPerson.js(7,18): BCE0018: The name '$$anonymous$$ouseLook' does not denote a valid type ('not found').

avatar image Bunny83 · Feb 22, 2014 at 04:58 AM 0
Share

@darix989: Well, if you got this error then you probably don't have the $$anonymous$$ouseLook script in your project, right? Try importing it from the Assets menu. Just click Assets --> Import Package --> CharacterController (because this package contains the $$anonymous$$oustLook script).

avatar image robertbu · Mar 03, 2014 at 04:44 AM 0
Share

This works also:

 Camera.main.GetComponent($$anonymous$$ouseLook).enabled = true; 

Not sure if there are any under-the-hood differences between this and the generic version.

avatar image darix989 · Mar 04, 2014 at 10:50 AM 0
Share

I already have the $$anonymous$$ouseLook in my project... I think to understand my problem and it's about compiling time. I don't know why the script compile in different time and they are not able to see each other. I "solved" the problem using the Send$$anonymous$$essage function and disabling the script with my boolean variable... It's not a good solution but it works

avatar image
1

Answer by aldonaletto · Oct 11, 2011 at 10:01 AM

I suspect that Unity doesn't know which type MouseLook is - the string version of GetComponent doesn't give any clue about type to the compiler. You can try this:

1) Use the type version:

 Camera.main.GetComponent(MouseLook).enabled = false;

2) If you get some kind of "Unknown type" error, use the ancestor type MonoBehaviour instead of MouseLook (MouseLook is C#, and your script is JS - C# and JS can't see each other unless one of them is already compiled):

 Camera.main.GetComponent(MonoBehaviour).enabled = false;

Like @timsk did, use Camera.main to get a reference to the main camera - avoid Find whenever you can, because it's a very slow operation.

Comment
Add comment · Show 2 · 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 Misto · Oct 11, 2011 at 05:07 PM 0
Share

Nope neither works. I can't seem to get enabled to work on anything, just throws a bunch of compile errors. It's really weird, nothing seems to work for me. I was trying to do some GUI stuff and nothing would work. Going to try a full reinstall of unity and start a new project and see if that fixes anything.

avatar image Bunny83 · Oct 11, 2011 at 05:09 PM 0
Share

@$$anonymous$$isto: don't use the Answer function if you want to comment. Answers should answer the question! I've converted your answer into a comment.

avatar image
0

Answer by timsk · Oct 11, 2011 at 08:12 AM

strange, cant really see anything wrong with what you have typed... try this:

 var mouseLook = Camera.main.GetComponent("MouseLook") = false;
 
 //in your pause script, add this:
 mouseLook.enabled = false;

I'm not 100% sure you can use GetComponent on Camera.main to be honest, if the above doesnt work, try this:

 var myCamera = GameObject.Find("MainCamera");

 //in the pause script put:
 myCamera.GetComponent("MouseLook").enabled = false;
Comment
Add comment · Show 2 · 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 aldonaletto · Oct 11, 2011 at 09:51 AM 0
Share

@timsk, you forgot a = false; appended to the first line!

avatar image Bunny83 · Oct 11, 2011 at 05:31 PM 0
Share

btw. this line:

 var mouseLook = Camera.main.GetComponent("$$anonymous$$ouseLook")

will statically type the mouseLook variable as a Component, so it doesn't change anything ;) If you declare your variable as $$anonymous$$ouseLook it's fine.

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

8 People are following this question.

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

Related Questions

Movement along X and Z axis... 2 Answers

Look rotation is "Snappy" 1 Answer

how to find mouse look 1 Answer

Top down shooter problem look exactly at mouse 0 Answers

Player Torso Rotation to mouselook. (Diagram included) 0 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