- Home /
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
How about trying this.
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Esc)) {
GetComponent<$$anonymous$$ouseLook>().enabled = false;
}
See if that works, then we can build on the logic.
Answer by tenukii · Apr 14, 2014 at 05:19 PM
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 ();
}
}
}
Answer by kennypu · Jan 31, 2011 at 11:25 PM
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.
This answer was posted while I was posting. It's the same thing, but isn't as performant.
sorry guys but neither of these scripts seem to work, were do i attatch them?
it should be attached to whatever object that has the mouselook script. $$anonymous$$ake sure you put in the Update function
Answer by Jessy · Jan 31, 2011 at 11:29 PM
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 don't see how yours is more performant is it is the same thing.
Two GetComponent calls.
Inefficient form of GetComponent. (string vs. generic)
sorry guys but neither of these scripts seem to work, were do i attatch them?
Answer by IBDelta · May 10, 2011 at 03:29 AM
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!
Answer by goldkillerv · Nov 07, 2012 at 12:52 PM
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.
GameObject.Find("First Person Controller").GetComponent($$anonymous$$ouseLook).enabled = false;
Isn't quite correct. It has to be:
GameObject.Find("First Person Controller").GetComponent("$$anonymous$$ouseLook").enabled = false;
@xwheat32x - your solution will not work. GetComponent() with a string returns a Component. A Component does not have an 'enabled' flag. The one you are trying to correct should work if the name in the Find() is a perfect match to the one-and-only object in the scene that has that exact name.
GameObject.Find("First Person Controller").GetComponent("$$anonymous$$ouseLook").enabled = false;
Unity no longer understands what this means... I don't get it.
Your answer
Follow this Question
Related Questions
Disable all instances of a component 2 Answers
Script to disable MouseLook? 2 Answers
How to Disable Mouse and Character movement? (Javascript) 1 Answer
Unable to disable the MouseLook script 2 Answers
Disable script from code 5 Answers