- Home /
Access the same component
Hi, I have 2 scripts that have different functions but both have something in common that they do, when clicked on the both access the First Person Controller and disable the mouse look scripts using this code teleport.GetComponent("MouseLook").enabled = false;
and both have the variable teleport defined in them using this code var teleport : GameObject;
however when both these scripts are applied only one works, but when removed the other works again. I am very new to unity and JavaScript but I think it has something to do with the variables being defined in both scripts. Here is the rest of my code for one of the scripts if that is needed.
var teleport : GameObject;
var ydisable : GameObject;
public var showstart : boolean = false;
public var showMessage : boolean = false;
var dialog : String = "";
public var showMessage2 : boolean = false;
var dialog2 : String = "";
public var showMessage3 : boolean = false;
var dialog3 : String = "";
public var showMessage4 : boolean = false;
var dialog4 : String = "";
public var showMessage5 : boolean = false;
var dialog5 : String = "";
function Start () {
}
function Update () { }
function OnGUI() {
if(showstart == true) {
teleport.GetComponent("MouseLook").enabled = false;
ydisable.GetComponent("MouseLook").enabled = false;
if(showMessage == true) {
if(GUI.Button (Rect((Screen.width/4)-100,
(Screen.height/4)-100, 600,300), dialog)) {
showMessage = false;
showMessage2 = true;
}
}
if(showMessage2 == true){
if(GUI.Button (Rect((Screen.width/4)-100,
(Screen.height/4)-100, 600,300), dialog2)) {
showMessage2 = false;
showMessage3 = true;
}
}
if(showMessage3 == true){
if(GUI.Button (Rect((Screen.width/4)-100,
(Screen.height/4)-100, 600,300), dialog3)) {
showMessage3 = false;
showMessage4 = true;
}
}
if(showMessage4 == true){
if(GUI.Button (Rect((Screen.width/4)-100,
(Screen.height/4)-100, 600,300), dialog4)) {
showMessage4 = false;
showMessage5 = true;
}
}
if(showMessage5 == true){
if(GUI.Button (Rect((Screen.width/-4)- -1500,
(Screen.height/4)- -100, 600,300), dialog5)) {
showMessage5 = false;
showstart = false;
}
}
}
if(showstart == false){
teleport.GetComponent("MouseLook").enabled = true;
ydisable.GetComponent("MouseLook").enabled = true;
}
}
function OnMouseDown () {
showstart = true;
showMessage = true;
}
Thanks for any help!
Answer by robertbu · Feb 25, 2014 at 09:12 PM
The issue is the order of execution of the scripts combined with the fact you are setting the 'enabled' flag on every frame. Actually since OnGUI() gets called multiple times per frame, you are setting the variable multiple times per frame.
I don't know which order the two scripts will have their OnGUI() called in, but let's assume that the above script gets called last. If the other script tried to change the 'enabled' state, your script above would overwrite the setting either on line 95 or on line 33.
Viewing the code out of context it is hard to give a firm recommendation for a 'best' fix, but a brute force solution is to do the enabling inside each GUI.Button() test. Anytime you potentially assign a new value to 'showstart' or 'showMessage', you would make the calls to enable/disable the appropriate 'MouseLook' script. That way, the 'enabled' flag would only be set when a button was clicked, not every frame.
Thanks for the answer, I think I understand why that fix would work but how exactly would I preform it? I've been using unity and javascript for about 3 weeks so I'm very much a newbie lol.
Edit: Never $$anonymous$$d, I read through your answer a few times and I figured it out! Thanks for your help!
Your answer
Follow this Question
Related Questions
how do I find components through variable 1 Answer
Updating variable on another script. It is not working. 2 Answers
GetComponent in another script help 1 Answer
How to compare variable component types between GameObjects at run time? 1 Answer
Accessing components via script and assigning randomized values to their parameters? 1 Answer