- Home /
Accessing variables from different scripts.
I know this has been asked multiple times but nothing has worked for me so far...i tried GetComponent but i got errors, i used these lines :
var options: Options = GetComponent(Options);
options.WaitTime;
What i'm trying to do is access the WaitTime variable from the "Options" script so i can use it with the "Play" script.
my code so far (regarding the WaitTime variable)on the "Play" script is this :
var waitTime: Options = GetComponent(Options);
waitTime.WaitTime;
function wait(){
yield WaitForSeconds(waitTime);
lol = false;
(i call the wait function through "OnMouseExit")
AND ON MY OPTIONS SCRIPT IT IS THIS:
static var WaitTime : int;
function wait(){
yield WaitForSeconds(WaitTime);
lol = false;
}
function Update(){
if( WaitTime < 0){
WaitTime= 0;
}
if( WaitTime >60){
WaitTime= 60;
}
}
(Again, i call the wait function on "OnMouseExit")
In my "Options" script i have the variable WaitTime and i want this variable to have the same value on both scripts ( and a 3rd one later on ).When i use those scripts i get 2 errors though:
1.Assets/MyScripts/Play.js(75,21): BCE0024: The type 'UnityEngine.WaitForSeconds' does not have a visible constructor that matches the argument list '(Options)'.
2.Assets/MyScripts/Play.js(4,15): BCE0034: Expressions in statements must only be executed for their side-effects.
I don't know if i was clear to what i wanted but, thanks in advance!!!
here yield WaitForSeconds(waitTime); you are passing gameobject where it is expecting float. Pass float value e.g waitTime.WaitTime
Hey, You have to use static variable to use in multiple scripts. For that you have to initialize a variable with "static" keyword. And now in play script u have to use like this: className.variableName in other class Option.WaitForSeconds=0; like this.
it worked with pushpak's and Glauco's replies combined, thank you
Answer by iRaMb0 · Jan 04, 2013 at 07:28 PM
Thank you guys for your replies , i solved the problem :D
in the pIay script i put these Iines:
var waitTime: Options = GetComponent(Options);
//a bunch of code here
function wait(){
yield WaitForSeconds(waitTime.WaitTime);
lol = false;
}
plus...i used what Gauco Beini Ramos Filho mentioned as well.
So thank you all, for making my day
Answer by fafase · Jan 04, 2013 at 08:17 PM
You know this questions has many answers on Unity Answers or even Unity Forum or even articles here http://unitygems.com/
One thing to keep in mind, avoid static even though you have been told to. This is not because it would not work here but more likely because you seem not to know what they are for yet and they would probably make you think they are for this purpose when they are not.
As for now, you have a target script and a fetcher script.
Target.js
var targetVar: AnyType;
Fetcher.js
var scriptReference : Target;
function Start(){
// If script on different object
scriptReference = GameObject.Find("ObjectName").GetComponent(Target);
// If script on same object
scriptReference = gameObject.GetComponent(Target);
}
function KungFoo(){
scriptReference.targetVar = value;
}
Answer by gbelini · Jan 04, 2013 at 10:48 AM
Hey, try this:
public static var WaitTime : int;
You should not use static unless you specifically mean for there to be only one instance. It's not a substitute for GetComponent.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085838im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
modifying scripts for mobile 0 Answers
Help Me with my gun! 1 Answer
How do you return variables from other scripts? 2 Answers
Hover Script problem 0 Answers