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.

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.

I can’t even count how often something like this has been asked :smiley:

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 :wink:

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;
   // ...
}

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;