Pixero
February 2, 2010, 1:02pm
1
I’ve searched the forum and tried whatever solutions I’ve found but i still can’t disable Mouse Look from my script.
This is what I want:
When User press “I” GUI is visable and Mouse Look disabled.
When User clicks OK button GUI goes away and Mouse Look is enabled again.
Here’s my script so far:
var settingsVisible : boolean;
var gameobj : GameObject;
var mouseL : MouseLook;
gameobj=GameObject.Find("First Person Controller");
mouseL = gameobj.GetComponent(MouseLook);
function Update () {
if (Input.GetKeyDown (KeyCode.I)) {
settingsVisible = true;
if(mouseL) {
mouseL.enabled = false;
}
}
}
function OnGUI () {
if( settingsVisible ) {
GUI.Box (Rect (10,10,108,280), "Inställningar");
GUI.Box (Rect (16,34,96,210), "Kvalitet");
if (GUI.Button (Rect (24,65,80,20), "Bäst")) {
QualitySettings.currentLevel = QualityLevel.Fantastic;
}
if (GUI.Button (Rect (24,95,80,20), "Bättre")) {
QualitySettings.currentLevel = QualityLevel.Beautiful;
}
if (GUI.Button (Rect (24,125,80,20), "Bra")) {
QualitySettings.currentLevel = QualityLevel.Good;
}
if (GUI.Button (Rect (24,155,80,20), "Enkel")) {
QualitySettings.currentLevel = QualityLevel.Simple;
}
if (GUI.Button (Rect (24,185,80,20), "Snabb")) {
QualitySettings.currentLevel = QualityLevel.Fast;
}
if (GUI.Button (Rect (24,215,80,20), "Snabbast")) {
QualitySettings.currentLevel = QualityLevel.Fastest;
}
if (GUI.Button (Rect (24,255,80,20), "OK")) {
settingsVisible = false;
}
}
}
var settingsVisible : boolean;
var gameobj : GameObject;
var mouseL : MouseLook;
function Awake()
{
gameobj =GameObject.Find("First Person Controller");
mouseL = gameobj.GetComponent(MouseLook);
}
function Update () {
if (Input.GetKeyDown (KeyCode.I)) {
settingsVisible = true;
if(mouseL.enabled) {
mouseL.enabled = false;
}
}
}
function OnGUI () {
if( settingsVisible ) {
GUI.Box (Rect (10,10,108,280), "Inställningar");
GUI.Box (Rect (16,34,96,210), "Kvalitet");
if (GUI.Button (Rect (24,65,80,20), "Bäst")) {
QualitySettings.currentLevel = QualityLevel.Fantastic;
}
if (GUI.Button (Rect (24,95,80,20), "Bättre")) {
QualitySettings.currentLevel = QualityLevel.Beautiful;
}
if (GUI.Button (Rect (24,125,80,20), "Bra")) {
QualitySettings.currentLevel = QualityLevel.Good;
}
if (GUI.Button (Rect (24,155,80,20), "Enkel")) {
QualitySettings.currentLevel = QualityLevel.Simple;
}
if (GUI.Button (Rect (24,185,80,20), "Snabb")) {
QualitySettings.currentLevel = QualityLevel.Fast;
}
if (GUI.Button (Rect (24,215,80,20), "Snabbast")) {
QualitySettings.currentLevel = QualityLevel.Fastest;
}
if (GUI.Button (Rect (24,255,80,20), "OK")) {
settingsVisible = false;
mouseL.enabled = true;
}
}
}
You may also want to disable the MouseLook script on the camera as well.
Pixero
February 2, 2010, 2:55pm
3
Thanks for looking into it, but it doesn’t work for me?
Also, what do you mean I should disable the MouseLook script on the camera?
I thought that was what I was doing?
(Sorry for beeing such a noob but I’m just learning to script in Unity.)
If you look at the First Person Controller prefab you’ll see there is one MouseLook script component attached handling the X axes (which is the one you are currently enabling/disabling) and one attached to the child “Main Camera” to handle the Y Axes. I believe this is done to keep the FPC pointing in the direction you are looking.
Pixero
February 2, 2010, 5:57pm
5
Ok, I got the part that also the camera has a MouseLook script that I must disable but I can’t find the right way of getting it. I keep getting a:
NullReferenceException: Object reference not set to an instance of an object
settingsScript.Update () (at Assets\settingsScript.js:24)
And the script:
private var settingsVisible : boolean;
private var gameobj : GameObject;
private var mouseL : MouseLook;
private var cam : GameObject;
private var mouseL2 : MouseLook;
function Awake()
{
gameobj =GameObject.Find("First Person Controller");
mouseL = gameobj.GetComponent(MouseLook);
cam = GameObject.Find("MainCamera");
mouseL2 = Camera.main.GetComponent(MouseLook);
}
function Update () {
if (Input.GetKeyDown (KeyCode.I)) {
settingsVisible = true;
if(mouseL.enabled) {
mouseL.enabled = false;
}
if(mouseL2.enabled) {
mouseL2.enabled = false;
}
}
}
function OnGUI () {
if( settingsVisible ) {
GUI.Box (Rect (10,10,108,280), "Inställningar");
GUI.Box (Rect (16,34,96,210), "Kvalitet");
if (GUI.Button (Rect (24,65,80,20), "Bäst")) {
QualitySettings.currentLevel = QualityLevel.Fantastic;
}
if (GUI.Button (Rect (24,95,80,20), "Bättre")) {
QualitySettings.currentLevel = QualityLevel.Beautiful;
}
if (GUI.Button (Rect (24,125,80,20), "Bra")) {
QualitySettings.currentLevel = QualityLevel.Good;
}
if (GUI.Button (Rect (24,155,80,20), "Enkel")) {
QualitySettings.currentLevel = QualityLevel.Simple;
}
if (GUI.Button (Rect (24,185,80,20), "Snabb")) {
QualitySettings.currentLevel = QualityLevel.Fast;
}
if (GUI.Button (Rect (24,215,80,20), "Snabbast")) {
QualitySettings.currentLevel = QualityLevel.Fastest;
}
if (GUI.Button (Rect (24,255,80,20), "OK")) {
settingsVisible = false;
mouseL.enabled = true;
mouseL2.enabled = true;
}
}
}
try:
function Awake()
{
gameobj =GameObject.Find("First Person Controller");
mouseL = gameobj.GetComponent(MouseLook);
cam = gameobj.Find("First Person Controller/Main Camera");
mouseL2 = cam.GetComponent(MouseLook);
}
Pixero
February 2, 2010, 7:02pm
7
That gives me exactly the same error.
Line 24 is the line which starts with:
if(mouseL.enabled) {
mouseL.enabled = false;
}
Probably a dumb question but do you have a FPC called “First Person Controller” in the scene hierarchy? Are you instantiating one in a script?
Pixero
February 2, 2010, 7:56pm
9
I use the First person controller from standard assets.
Not that I know of.
Well what you are describing would be the effect you would get if the MouseLook script on the FPC was not yet initialized when you tried to get a reference to it:
mouseL = gameobj.GetComponent(MouseLook);
However as far as I know all objects are initialized before the awake function is called, but you could try changing the Awake function to a Start function.
Try the following:
Create a new empty project and import the attached package into it and open the example scene and run. Do you still get errors?
258506–9291–$test_698.unitypackage (18 KB)
Pixero
February 3, 2010, 8:34am
11
Thanks, the test scene works as it should.
Now, how can I make it work with my scene/project?
Creating a new first person controller didnt work.
Changing: function Awake() to function Start() didn’t either.
Thanks, the test scene works as it should.
Now, how can I make it work with my scene/project?
Creating a new first person controller didnt work.
Changing: function Awake() to function Start() didn’t either.
All I did was this:
in the mouselook script, add a line:
public static bool mouseLock = false;
And in the void Update function:
if (!mouselock) {
//everything that was in Update before
}
Then, in your disabler/menu script or whatever (javascript)
var script : MouseLook;
script.mouseLock = true; //to lock it, assumedly you'd put this by your "Menu is open" in the OnGui function
script.mouseLock = false; //to unlock, put where the button "closes" the menu
Yeah, worked for me. Probably a more elegant solution somewhere.