Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by HenryChinaski · Jul 11, 2014 at 02:37 AM · coroutine

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Jul 10, 2014 at 10:59 PM 0
Share

That shouldn't normally be a problem. Are these scripts both in the same folder?

avatar image HenryChinaski · Jul 10, 2014 at 11:39 PM 0
Share

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?

avatar image Kiwasi · Jul 11, 2014 at 02:39 AM 0
Share

If folders are the problem it might be because you are using one of the special folders?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HenryChinaski · Jul 11, 2014 at 10:39 AM 0
Share

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?

avatar image Bunny83 · Jul 11, 2014 at 11:15 AM 0
Share

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;
     }
 }
avatar image HenryChinaski · Jul 11, 2014 at 12:32 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges