- Home /
variable scope problem
I would like to invoke from another script ( Switch ) a boolean called "opened". I would like to know what I am doing wrong as my variable "open" it's not taking value true.
#pragma strict
var open;
var opened : boolean = false;
function Start () {
var yes : Switch = GetComponent(Switch);
open = yes.opened;
}
function Update () {
if( open == true){
animation.Play("RockMove");
opened = true;
}
}
Thank You.
EDIT
:I have tryied everything I could
static .1
-Use-and142062-d.com/threads/3http://forum.unity .2
change-a-variable-of-another-script function Start () { // Find the OtherScript which is .3 : attached to any game object in the scene. var other ;)OtherScript = FindObjectOfType(OtherScript
} ;)(other.DoSomething
Q66Scripts : http://pastebin.com/QFLzS Yftk7http://pastebin.com/DkH
Declaring yes before start function will cause an error that will say to declare it in start, and it won't play the animation 500 times per sec. Yes it's a poor name but I was just testing. I hate global variables.
I have tryied everything I could:
static
http://forum.unity3d.com/threads/142062-Use-and-change-a-variable-of-another-script
function Start () { // Find the OtherScript which is attached to any game object in the scene. var other : OtherScript = FindObjectOfType(OtherScript); other.DoSomething(); }
Scripts : http://pastebin.com/QFLzS66Q
Answer by Fattie · Oct 08, 2012 at 05:26 AM
try this champ
#pragma strict
private var otherScript :Start;
function Awake()
{
otherScript = GetComponent(Switch);
if( otherScript == null ) Debug.Log("SOMETHING IS WRONG");
}
function Update ()
{
if( otherScript.opened )
Debug.Log("the variable is true");
else
Debug.Log("the variable is false");
}
I am sorry to say but we are noobs. Thanks to your logs I saw that the variable was not taking value of true.
In the first script
opened = isOpened;
in Awake function, that means it will stay forever false. Just had to put it in Update.
$$anonymous$$an calm down I'm not that mean, I pressed after I got it but porobably didn't worked :))
Not as nuts as people who consistently post answers as comments, making it impossible for the question-asker to accept the answer, and who always delete their comments, making for comment threads that make no sense. cough ::looks at Fattie meaningfully::
Your answer
Follow this Question
Related Questions
fix script to get C# var from another object instead of object this script is attached to 2 Answers
Can 2 scripts attached to one object communicate with each other? 2 Answers
Stupid question about public / private variables 2 Answers
Script is setting variables but they're being reset 1 Answer
How do I access a script variable from a class defined within that script? 1 Answer