- Home /
Get static var inside coroutine (C#)
Hey guys,
Ive got the following code in one script called "Script1":
public static int testVar=0;
And this code in my other script called "Script2":
void Start()
{
StartCoroutine(doSomething());
}
IEnumerator doSomething(){
if (Script1.testVar==1)
{
(...)
}
}
I get the error that Script1 would not exist in the current context. So am I right, that it is not possible to get a static var from another script inside a coroutine? How should I solve this problem?
That shouldn't normally be a problem. Are these scripts both in the same folder?
No, actually they werent and that seems to be the problem. But I am using a lot of relating script so if I move Script1 to the folder with Script2 I get the same errors again, just for other Scripts. Is there anything more easy to do than try to fix this by moving all my scripts around?
If folders are the problem it might be because you are using one of the special folders?
Answer by Bunny83 · Jul 11, 2014 at 02:46 AM
You should read about script compilation order and groups. Scripts which are places in the first group are compiled first. That means they can't access anything which is compiled in a later group since they aren't compiled yet. The second group has access to everything in it's own group and the group before. Since the first group is already compiled, the compiled assembly can be referenced when compiling the second group. this goes on like this up to the last group.
Group 1 and 3 are your actual game scripts. Group 2 and 4 are only for editor scripts.
I guess you have places some scripts either in the "plugins" folder or "Standard Assets" folder. That will cause them being compiled before the "normal" script which are outside of those folders.
Thanks for your reply. You were totally right, one of them is in the Plugins folder. It is part of an asset I use. If I put it out, or even if I put the whole folder out of the Plugins section, I get tons of errors regarding the asset... You really helped me a lot understanding the compilation mechanics, but is there anything I can do to get my static var from outside the plugins folder, in to my script inside the plugins folder? $$anonymous$$aybe through GameObject.Send$$anonymous$$essage?
Well, i wouldn't use sendmessage for that as it can't return values..
The easiest fix is: move your static variable into the script in the plugins folder. When the plugins scripts are compiled all the other classes outside don't exist yet, so there's no way to directly access such a class.
You really shouldn't create a dependency of a plugins script on a script outside the plugins folder. Things in the plugins folder should live on their own. If you need to communicate back to a unknown caller / user you can provide an interface which the plugin can use and the user has to implement and provide it to the plugin.
example:
// inside the plugins folder:
public interface ITestVarProvider
{
int TestVar {get;set;}
}
// in your $$anonymous$$onoBehaviour in the plugins folder:
public class Script2 : $$anonymous$$onoBehaviour
{
public ITestVarProvider tVProvider = null;
void Start()
{
StartCoroutine(doSomething());
}
IEnumerator doSomething(){
if (tVProvider != null && tVProvider.TestVar==1)
{
//(...)
}
}
}
// in your normal class outside of the plugins folder:
public class Script1 : $$anonymous$$onoBehaviour, ITestVarProvider
{
public static int testVar=0;
public int TestVar
{
get { return Script1.testVar; }
set { Script1.testVar = value; }
}
void Awake()
{
// get a reference to the instance of Script2
Script2 instance = // (FindObject...) or (GetComponent...) or (public variable ...);
// since this class implements the ITestVarProvider interface, it can be
// assigned to the tVProvider variable of your plugin script.
instance.tVProvider = this;
}
}
Well, it works perfectly. The solution to simply change the variables location was in retrospective quite obvious but I couldnt come up with this after hours of trying... Thanks a lot for your help and all your further and general explanation!
Your answer
Follow this Question
Related Questions
Working projectile scripts do not work due to if condition 1 Answer
Method keeps looping when using waitForSeconds 1 Answer
Segment within a nested If does not get evaluated 2 Answers
Make a coroutine run when Time.timeScale = 0; 3 Answers
convert timer from update() to coroutine or invokerepeating 0 Answers