- Home /
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.
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.
Use the generic version of
GetComponent.<MouseLook>()
this will return a reference of the type you've given.Cast it implicit into the correct type by assigning the returned reference to a variable of the correct type.
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;
// ...
}
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.
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').
@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).
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.
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
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.
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.
@$$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.
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;
@timsk, you forgot a = false; appended to the first line!
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
Follow this Question
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