disabling a script (MouseLook)

when pausing my mouse look script is still active, how do i disable it, i tried using this script and butting it on the same object with the script attached but nothing...

if (Input.GetButtonUp("Esc"))
GetComponent("MouseLook").enabled = false;
else if (Input.GetButtonUp("Esc"))
GetComponent("MouseLook").enabled = true;

Thanks alot :D

Example code for a main menu that toggles on escape key and turns off mouse look in C#. Of course you have to traverse whatever object hierarchy you have, not just blindly copy/paste. ps: Their are two mouse look scripts, one for the player and one for the player camera, the reason is you don’t want the player to rotate in the up/down direction, just rotate around the y axis - so the scripts are slightly different.

using UnityEngine;
using System.Collections;

public class MainMenu : MonoBehaviour
{
	public bool displayMenu = true;
	public Texture mainMenuImage;
	MouseLook playerLook;
	MouseLook playerCameraLook;
	Rect windowRect;
	// Use this for initialization
	void Start ()
	{
		playerLook = (MouseLook)GameObject.Find ("First Person Controller").GetComponent ("MouseLook");
		playerCameraLook = (MouseLook)GameObject.Find ("Main Camera").GetComponent ("MouseLook");
		playerLook.enabled = !displayMenu;
		playerCameraLook.enabled = !displayMenu;
	}
	// Update is called once per frame
	void Update ()
	{
		if (Input.GetKeyDown (KeyCode.Escape)) {
			displayMenu = !displayMenu;
			playerLook.enabled = !playerLook.enabled;
			playerCameraLook.enabled = !playerCameraLook.enabled;
		}
	}

	void OnGUI ()
	{
		if (displayMenu) {
			windowRect = GUI.Window (0, new Rect (Screen.width / 4, Screen.height / 4, 2 * Screen.width / 4, 2 * Screen.height / 4), DrawMainMenu, "Ambient Main");
		}
	}

	void DrawMainMenu (int windowID)
	{
		GUILayout.Label (mainMenuImage);
		if (GUILayout.Button ("Quit")) {
			Application.Quit ();
		}
	}
}

this would be better:

if(Input.GetKeyUp(KeyCode.Escape))
  GetComponent("MouseLook").enabled = !GetComponent("MouseLook").enabled

what that does is it'll flip the true and false. so if its true, it'll be come false, and vice versa. Your script didn't wprk because both of those statements will be true all the time. It's like asking, if 1 ==1, do this, else if 1 == 1, do this. both are true, so in the end, it'll just be set to enabled again.

There's not much that's right about that script. Try this instead, and let us know if that's what you're talking about.

function Update () {
    if (Input.GetKeyUp(KeyCode.Escape)) {
        var mouseLook = GetComponent.<MouseLook>();
        mouseLook.enabled = !mouseLook.enabled;
    }
}

I think what these fine people fail to realize is that the FPS controller has two MouseLook scripts attached, one only appears to control the vertical for some strange reason I may never fully understand. No offence intended, I'm no programmer, but we can all miss the woods for the trees on occasion. My solution was to edit the MouseLook script itself with a simple toggling boolean. The main body of the code was then wrapped in an if statement that only executes the code when boolean is true. You can change the Input.GetKey to whatever floats your boat :-)

Replace the entire contents of the MouseLook script with the following code and the looking behavior is toggled with the "m" key.

using UnityEngine;
using System.Collections;

    /// MouseLook rotates the transform based on the mouse delta.
    /// Minimum and Maximum values can be used to constrain the possible rotation

    /// To make an FPS style character:
    /// - Create a capsule.
    /// - Add the MouseLook script to the capsule.
    ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
    /// - Add FPSInputController script to the capsule
    ///   -> A CharacterMotor and a CharacterController component will be automatically added.

    /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
    /// - Add a MouseLook script to the camera.
    ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
    [AddComponentMenu("Camera-Control/Mouse Look")]
    public class MouseLook : MonoBehaviour {

        public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
        public RotationAxes axes = RotationAxes.MouseXAndY;
        public float sensitivityX = 15F;
        public float sensitivityY = 15F;

        public float minimumX = -360F;
        public float maximumX = 360F;

        public float minimumY = -60F;
        public float maximumY = 60F;

        float rotationY = 0F;

        bool MouseActive=true;

        void Update ()
        {       
            if(Input.GetKeyDown("m"))
            {
                MouseActive = !MouseActive;
            }
            if(MouseActive)
            {
                if (axes == RotationAxes.MouseXAndY)
                {
                    float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

                    rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                    rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

                    transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
                }
                else if (axes == RotationAxes.MouseX)
                {
                    transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
                }
                else
                {
                    rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                    rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

                    transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
                }
            }
        }

        void Start ()
        {
            // Make the rigid body not change rotation
            if (rigidbody)
                rigidbody.freezeRotation = true;
        }
    }

Good Luck!

function Update()

{

    if (Input.GetKeyDown ("Esc"))

    {

        GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;

    }

    else

    {

        GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;

}

I am pretty sure that this will work.

using UnityEngine.SceneManagement;
using UnityEngine;

public class PausedMenu : MonoBehaviour
{
public GameObject Pause_Menu;
public AtesEtme ateset;
public Pistol pistol;
public UnityStandardAssets.Characters.FirstPerson.FirstPersonController player;
bool isPaused;

private Vector3 lastMousePosition;

private void Awake()
{
    Cursor.lockState = CursorLockMode.None;
    Time.timeScale = 1;
    Cursor.visible = true;
    pistol = GetComponentInChildren<Pistol>();
}

void Update()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        isPaused = !isPaused;
    }

    if (isPaused)
    {
        Pause_Menu.SetActive(true);
        Time.timeScale = 0;
        pistol.enabled = false;
        player.enabled = false;
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }
    else
    {
        Pause_Menu.SetActive(false);
        Time.timeScale = 1;
        player.enabled = true;
        pistol.enabled = true;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    
}

public void ResumeGame()
{
    isPaused = false;
}

public void ExitGame()
{
    Application.Quit();
}

public void MainMenu()
{
    Time.timeScale = 1;
    SceneManager.LoadScene(1);
}

}