- Home /
Unable to disable the MouseLook script
I have a section of code that i want to disable the mouse script when the button I is pressed. I know there is a better way to do it, such as making a variable for GUI and a funciton such as showGUI = !showGUI, but im not 100% sure on how to set that up with disabling all the moving scripts.
I am using the old MouseLook and Character controller script because i despise the new one. I also think these are easier to work with.
Errors: NullReferenceException: Object reference not set to an instance of an object Inventory.Update () (at Assets/Scripts/Inventory.js:92)
NullReferenceException: Object reference not set to an instance of an object Inventory.Update () (at Assets/Scripts/Inventory.js:82)
Section of code line 5=line 82 (It had no problem on the one directly after)
if(showGUI == true)
{
Time.timeScale = 0;
GameObject.Find("First Person Controller").GetComponent(FPSInputController).enabled = false;
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
GameObject.Find("Main Camera").GetComponent(MouseLook).enabled = false;
GameObject.Find("Arms05").GetComponent(PlayerControl).enabled = false;
GameObject.Find("Main Camera").GetComponent(RayCastChop).enabled = false;
}
if(showGUI == false)
{
Time.timeScale = 1;
GameObject.Find("First Person Controller").GetComponent(FPSInputController).enabled = true;
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;
GameObject.Find("Main Camera").GetComponent(MouseLook).enabled = true;
GameObject.Find("Arms05").GetComponent(PlayerControl).enabled = true;
GameObject.Find("Main Camera").GetComponent(RayCastChop).enabled = true;
}
}
Do you have two $$anonymous$$ouseLook components, one attached to a gameObject named "First Person Controller" and another attached to a gameObject named "$$anonymous$$ain Camera"?
from doc :
it is better to use GetComponent with a Type ins$$anonymous$$d of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name ins$$anonymous$$d of type.
so shouldn't it be GetComponent("$$anonymous$$ouseLook")
?
this might work, but then i cannot disable it by putting .enabled = false unless there is another way.
Answer by Boggs · Jun 30, 2015 at 10:59 PM
The only way I got it to work was to modify the MouseLook.CS script by adding 2 ifStatements to the X & Y Sensitivity floats to "0" and 2 ifStatements to return value back to "2". Hope this helps if someone has a better way plz share.
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Characters.FirstPerson
{
[Serializable]
public class MouseLook
{
public float XSensitivity = 2f;
public float YSensitivity = 2f;
public bool clampVerticalRotation = true;
public float MinimumX = -90F;
public float MaximumX = 90F;
public bool smooth;
public float smoothTime = 5f;
private Quaternion m_CharacterTargetRot;
private Quaternion m_CameraTargetRot;
public void Init(Transform character, Transform camera)
{
m_CharacterTargetRot = character.localRotation;
m_CameraTargetRot = camera.localRotation;
}
public void LookRotation(Transform character, Transform camera)
{
float yRot = CrossPlatformInputManager.GetAxis ("Mouse X") * XSensitivity;
float xRot = CrossPlatformInputManager.GetAxis ("Mouse Y") * YSensitivity;
m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
if (Input.GetMouseButtonDown (2))
Debug.Log (YSensitivity = 0f);
if (Input.GetMouseButtonDown (2))
Debug.Log (XSensitivity = 0f);
if (Input.GetMouseButtonDown (1))
Debug.Log(YSensitivity = 2f);
if (Input.GetMouseButtonDown (1))
Debug.Log(XSensitivity = 2f);
if (clampVerticalRotation)
m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
if(smooth)
{
character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
smoothTime * Time.deltaTime);
camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
smoothTime * Time.deltaTime);
}
else
{
character.localRotation = m_CharacterTargetRot;
camera.localRotation = m_CameraTargetRot;
}
}
Quaternion ClampRotationAroundXAxis(Quaternion q)
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
return q;
}
}
}
Answer by siblingrivalry · Jul 05, 2015 at 09:23 PM
Hi, are you using unity4 First Person Character controllers inside Unity5?
If so, just move the Unity4 character controller in to the "Standard Assets" folder where the Unity 5 controller lives.
Then MouseLook is working without needing to hack the Mouselook.CS
hope that helps.
-original answer by @SG|Gamer
Your answer
Follow this Question
Related Questions
Disable all instances of a component 2 Answers
Script to disable MouseLook? 2 Answers
disabling a script (MouseLook) 5 Answers
How to Disable Mouse and Character movement? (Javascript) 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers