I am trying to access the MouseLook Script attached to the Main Camera to stop the movement while an inventory screen is up. I am able to access the one attached to the first person controller but not the one attached to the Main Camera. I even tried to make a copy of the script under a different name but no luck. It tells me it is not valid when I try to access it like I do the MouseLook attached to the First Person Controller. Here is the example of the original MouseLook Script.
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;
void Update ()
{
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;
}
}
Here is the code I am using to access the script. Keep in mind I have made a copy of the script and renamed it to Mouse.
#pragma strict
//Private variables
private var InventoryOn = false;
private var scrollBarChopGrid : Vector2 = Vector2.zero;
private var GridValue : float = -1;
//Gui Pos/Size
var ClosePosition : Vector2 = new Vector2(312,5);
var CloseSize : Vector2 = new Vector2(35,35);
var NamePosition : Vector2 = new Vector2(13,13);
var NameSize : Vector2 = new Vector2(250,250);
var GridPosition : Vector2 = new Vector2(10,2);
var GridSize : Vector2 = new Vector2(323,410);
var ScrollPosition : Vector2 = new Vector2(0,95);
var ScrollSize : Vector2 = new Vector2(353,257);
var WindowPosition : Vector2 = new Vector2(0,0);
var WindowSize : Vector2 = new Vector2(360,360);
var DragWindowPosition : Rect = Rect(0, 0, WindowSize.x, WindowSize.y);
//Textures
var InventoryWindow : Texture;
var CloseIcon : Texture;
var Grids : Texture[];
function Start () {
}
function Update ()
{
//On or Off
if (Input.GetKeyUp ("i"))
{
if (InventoryOn == false)
{
InventoryOn = true;
Screen.lockCursor = false; // unlock the mouse
var mouseLook = GetComponent(MouseLook); //getting mouselook script and disabling it
mouseLook.enabled = false;
var mouseLook2 = GetComponentInChildren(MouseLook); //getting FPS (movement) script and disabling it
mouseLook2.enabled = false;
var motor = GetComponent(CharacterMotor); //getting FPS (movement) script and disabling it
motor.enabled = false;
}
else if (InventoryOn == true)
{
InventoryOn =false;
Screen.lockCursor = true;
mouseLook = GetComponent(MouseLook); //getting mouselook script and reenabling it
mouseLook.enabled = true;
mouseLook2 = GetComponentInChildren(MouseLook); //getting FPS (movement) script and disabling it
mouseLook2.enabled = true;
motor = GetComponent(CharacterMotor); //getting FPS (movement) script and disabling it
motor.enabled = true;
scrollBarChopGrid.y = 0;
DragWindowPosition.x = 10;
DragWindowPosition.y = 10;
}
}
}
function OnGUI()
{
if (InventoryOn == true)
{
DragWindowPosition = GUI.Window(0, DragWindowPosition, DoMyWindow, "");
}
}
function DoMyWindow(windowID : int)
{
if (InventoryOn == true)
{
GUI.BeginGroup(new Rect(WindowPosition.x, WindowPosition.y, WindowSize.x, WindowSize.y), InventoryWindow);
//Name
GUI.Label(Rect(NamePosition.x, NamePosition.y, NameSize.x, NameSize.y),"Your Inventory");
//Close Button
if (GUI.Button(Rect(ClosePosition.x, ClosePosition.y, CloseSize.x, CloseSize.y),CloseIcon))
{
InventoryOn = false;
Screen.lockCursor = true;
var mouseLook = GetComponent(MouseLook);
mouseLook.enabled = true;
//var fps = GetComponent(FPSInputController);
//fps.enabled = true;
var motor = GetComponent(CharacterMotor); //getting FPS (movement) script and disabling it
motor.enabled = true;
}
//Scroll Bar
scrollBarChopGrid = GUI.BeginScrollView(Rect (ScrollPosition.x, ScrollPosition.y, ScrollSize.x, ScrollSize.y), scrollBarChopGrid, Rect(0,0,0,420));
GridValue = GUI.SelectionGrid(Rect(GridPosition.x, GridPosition.y , GridSize.x, GridSize.y), GridValue, Grids, 5);
GUI.EndScrollView();
//Dragable window
GUI.DragWindow (Rect (WindowPosition.x, WindowPosition.y, 10000, 40));
GUI.EndGroup();
}
}
Just in case it matters here is the renamed script.
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")]
public class Mouse: 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;
void Update ()
{
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;
}
}
Anyone know if I am doing something wrong or if there is another way for me to access it. I have tried both GetComponent and GetComponentInChild.