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
0
Question by AntLewis · Dec 04, 2011 at 06:12 AM · staticstartcoroutine

Static function, coroutine and trying to access an instance

Hiya, I have a static function issue/ IEnumerator issue. I have a static function called RegisterSelection:

 public static void RegisterSelection (GameObject objectToRegister)
 {
 //code
 StartCoroutine ( MoveAndCheckForMatch () );
 }

…within this function I"m trying to execute StartCoroutine called MoveAndCheckForMatch as you can see.

  private IEnumerator MoveAndCheckForMatch ()
 {
 //code
 }

And I guess you know what's coming. Initially the compiler was complaining that 'an object reference is required to access non-static member SphereController.MoveAndCheckForMatch etc etc I understand this to be the case, as coroutines can only run on instances of monobehaviours

SphereController is attached to a gameobject in the world, so after reading the forums I tried to get access to an instance of the script:

  instanceReference = GameObject.Find("Controller").GetComponent<SphereController>();

and tried to change how I'm calling the coroutine to

 instanceReference.StartCoroutine ( MoveAndCheckForMatch () );

However I'm still getting the same issue. I know this is a commonly asked question on the forums, but I understood that i was following the suggested solution, but I'm clearly messing up somewhere!

Thanks in advance for any assistance.

Comment
Add comment · Show 1
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 syclamoth · Dec 04, 2011 at 06:14 AM 0
Share

I apologise for this incredibly slow moderation queue today. It's been offline for the last 14 hours or so, and this is the first moment I've been able to access it to clear people's post!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Dec 04, 2011 at 06:19 AM

The problem here is that 'MoveAndCheckForMatch' is a private member of SphereController, meaning you can't access it from outside! Instead, you should expose a public method which can be called directly, and then passes the message on.

 public void StartMoveAndCheck()
 {
     StartCoroutine(MoveAndCheckForMatch());
 }

 instanceReference.StartMoveAndCheck();

Static functions are a little bit annoying to get working in an object-oriented sense, since they kind of fly in the face of everything oop stands for. You should avoid them when you can.

Comment
Add comment · Show 1 · 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 AntLewis · Dec 04, 2011 at 08:58 AM 0
Share

thanks for your response, but I don't think this is the solution - it's not a question of visibility within the class (I can change the $$anonymous$$oveAndCheckFor$$anonymous$$atch to public) it's the fact that it needs an instance as it's a coroutine, which I thought 'instanceReference = GameObject.Find("Controller").GetComponent();' would resolve.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can I active some static function in ScriptableObject? 0 Answers

How can i use static function in ScriptableObject? 3 Answers

StartCoroutine in a public static void 1 Answer

Reset a static variable? 1 Answer

Script to change GUI text's displayed text value? 7 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