- Home /
I need to freeze Camera movement from FirstPersonController
I am getting "UnityStandardAssets.Characters.FirstPerson.MouseLook is a type but a variable was expected" error. How I fix this? I want to freeze my camera movement when a player enters Pause Menu.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class PauseMenu : MonoBehaviour {
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
} else
{
Pause();
}
}
}
void Resume ()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
GetComponent(MouseLook).enabled = true;
}
void Pause ()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
GetComponent(MouseLook).enabled = false;
}
}
GetComponent has a type-argument that uses a different syntax. Change those to look like this:
GetComponent<$$anonymous$$ouseLook>().enabled = true;
Although, it's better in the future if you just keep a reference of that within the script itself, ins$$anonymous$$d of getting that component each time -- like you're already doing with the UI.
If you aren't comfortable with the generic syntax for GetComponent, you may use
GetComponent(typeof($$anonymous$$ouseLook)).enabled = true;
in this case.
Visual Studio just says that "Component doesn't contain a definition for enabled etc...".
Answer by TreyH · Mar 14, 2018 at 05:00 PM
So, MouseLook isn't a component.
It's a helper class for Unity's standard FirstPersonController, and the FirstPersonController doesn't let you do that out-of-the-box. But, you can add that functionality pretty easily. Edit that FirstPersonControllerScript accordingly:
(Within FirstPersonController.cs) At the top, add a new variable:
// Add this to the top of FirstPersonController's variable declarations;
public bool mouseLookEnabled = false;
(Within FirstPersonController.cs) At the bottom, there will be a function called RotateView. Replace it with this:
private void RotateView ()
{
if (this.mouseLookEnabled)
m_MouseLook.LookRotation (transform, m_Camera.transform);
}
(Within PauseMenu.cs or wherever it's declared) At the top, add a new variable. You will need to assign this in the inspector before you run the scene.
public FirstPersonController firstPersonController;
Then replace your GetComponent().enabled calls with:
this.firstPersonController.mouseLookEnabled = true;
np! If the answer solved your problem, please mark it as the correct solution so that this thread can be closed.
Sure! The only thing is that i guess that CursorLock is disabled until i click once after I go back from Pause. Dont know if it will be visible in the end product, but i guess i deal with it later.
Sorry, but I have the same problem. I tried doing what you said but for some reason I get an error and I don't really understand where do m_$$anonymous$$ouseLook and the other vars come from. I am using exactly the same $$anonymous$$ouseLook script and Pause$$anonymous$$enu - by Brackeys, right? Can you copy your pause menu and mouse look and send them to me, because this way I might be able to undestand them once they are written. Thanks in advance! @TreyH
Your answer
Follow this Question
Related Questions
Stop Unity from freezing on NullReferenceExceptions ? 1 Answer
Object reference not set to an instance of an object. ( FirstPersonController) 1 Answer
Object not set to a instance of a object 1 Answer
Error with pause menu script. 0 Answers
Pause Menu Wont Disable Mouse,Pause menu wont disable Player Controls 0 Answers