- Home /
Solved One Problem, Got another
Okay So I was writing a script in which I was attempting to lock camera movement when a GUI pulls up. The problem I am having is that I have multiple GUI's that all need to affect camera movement, but when I apply the locking scripts to the script that displays the gui to multiple objects, only one out of the say, five objects actually works. The scripts don't break the gui's but they don't use the locking motion. Here's the two scripts below.
GUI Object 1: var information: String;
var script: MouseLookPlus;
var script2: MouseLookPlus;
private var guiOn = false;
private var rect: Rect;
function Mouse1 () {
script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);
script.enabled = false;
script2 = GameObject.Find("First Person Controller").GetComponent(MouseLookPlus);
script2.enabled = false;
}
function Mouse11 () {
script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);
script.enabled = true;
script2 = GameObject.Find("First Person Controller").GetComponent(MouseLookPlus);
script2.enabled = true;
}
function OnMouseDown () {
guiOn = true;
rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
}
function OnGUI () {
if (guiOn){
Mouse1();
GUI.Label(rect, information);
if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
guiOn = false;
}
if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
animation.Play ("dooropen1");
guiOn = false;
}
}
else {
Mouse11 ();
}
}
GUI Object 2:
var information: String;
var script: MouseLookPlus;
var script2: MouseLookPlus;
private var guiOn = false;
private var rect: Rect;
function Mouse2 () {
script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);
script.enabled = false;
script2 = GameObject.Find("First Person Controller").GetComponent(MouseLookPlus);
script2.enabled = false;
}
function Mouse22 () {
script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);
script.enabled = true;
script2 = GameObject.Find("First Person Controller").GetComponent(MouseLookPlus);
script2.enabled = true;
}
function OnMouseDown () {
guiOn = true;
rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
}
function OnGUI () {
if (guiOn){
Mouse2();
GUI.Label(rect, information);
if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
guiOn = false;
}
if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
animation.Play ("dooropen2");
guiOn = false;
}
}
else {
Mouse22 ();
}
}
I'm not getting any error, and the code renders, just doesn't work properly, are the locking scripts possibly conflicting with each other? Is there a script I can tie to just the first person controller when a gui is on? Help would be appreciated.
Answer by iwaldrop · Mar 25, 2013 at 11:46 PM
You shouldn't be calling methods from OnGUI, but it doesn't look like it would cause the problem you're describing. Also, calling GetComponent() is expensive, and you should probably just cache references to them in Awake().
You might try pulling your method calls out of OnGUI to see if that fixes your problem, but otherwise I'm afraid I don't understand what you're asking. It isn't really clear what you're trying to do here. If you want to decide whether or not to turn the GUI on then you should be doing that in your OnMouseDown() function. That way you're not turning it on just to decide whether you should leave it on.
Also, there doesn't seem to be any difference in these scripts aside from "dooropen1" and "dooropen2". I'd suggest making that a public string variable, setting it there, and trashing one or more of your scripts that does the same thing. Google DRY code. ;)
If you have any questions about any of this, reply to this answer as and we'll work it out.