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 frnsys · Oct 17, 2014 at 11:20 PM · coroutineinvoke

Coroutines and Invoke not working

Do coroutines and Invoke not work properly outside of the Start() and Update() methods?

I have been trying to very simply execute a method at some later time, but it never works.

With Invoke, I'm simply doing:

 class Research : MonoBehaviour {
     public void Begin() {
         Invoke("End", 1);
     }
     
     public void End() {
         Debug.Log("Ended");
     }
 }

With a coroutine, I've tried just doing:

 class Research : MonoBehaviour {
     public void Begin() {
         StartCoroutine(End());
     }
 
     IEnumerator End() {
         yield return new WaitForSeconds(1);
         Debug.Log("Ended");
         yield return null;
     }
 }

Then I have another class with a method which calls Begin(). I use an instance of this GameManager class to start things, i.e. by calling gameManager.Research().

 class GameManager : MonoBehaviour {
     public Research research;
 
     void Start() {
         research = gameObject.AddComponent<Research>();
     }
 
     public bool Research() {
         research.Begin();
     }
 }


I never see the "End" message get printed. I checked if the object was being destroyed, but it isn't. I haven't been able to figure out why such a simple example doesn't work. I have coroutines working fine elsewhere in my program but they are called from a Start() method, but I haven't been able to find any documentation that says coroutines are valid only at certain times.

Even within the Update() method, it seems if Invoke isn't called directly it doesn't work. To clarify, this works:

 void Update() {
     Invoke("End", 1);
 }


but this doesn't:

 public bool startInvoke = false;
 
 void Update() {
     if (startInvoke) {
         Invoke("End", 1);
     }
 }
 
 // something sets startInvoke = true;




To clarify, I am getting an instance of GameManager and calling Research on it, i.e. gameManager.Research(), which ends up calling Begin().

Comment
Add comment · Show 7
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 KpjComp · Oct 17, 2014 at 11:29 PM 0
Share

The bit were you say -> Then I'm starting things with:

$$anonymous$$aybe I'm missing something, but were in that code are you starting anything.?

avatar image frnsys · Oct 17, 2014 at 11:30 PM 0
Share

I added a clarification at the bottom, but I will edit the post again to make that language clearer. But outside of the code presented here, I am starting this by calling Research() on an instance of Game$$anonymous$$anager.

avatar image KpjComp · Oct 17, 2014 at 11:37 PM 0
Share

If you do a Debug.Log("start") in your Begin(), do you get this logged.?

avatar image frnsys · Oct 17, 2014 at 11:38 PM 0
Share

Yes, I assure you that the issue is not that the method isn't called.

avatar image MrSoad · Oct 17, 2014 at 11:40 PM 0
Share

I'm not a C# guy but I'm pretty mystified by this. Those bottom two code extracts are identical apart from the "startInvoke" bool. Why this would make any difference I have no idea... Replace this if bool line with :

 if (1 == 1) {

not that this should make a difference but there can be no argument that 1 does indeed equal 1... (Edit : and put a Debug.Log after it just to be totally certain you are getting past the if statement.)

Oh and as far I know from js there are no issues calling an Invoke or coroutine from Update() at all. That's part of what they are there for as you cannot yield Update but may need to yield something so you do it via calling a coroutine...???

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by 8Eye · Oct 18, 2014 at 03:32 AM

It looks like your not telling it to research. Maybe this will work?

 class GameManager : MonoBehaviour {
      public Research research;
  
      void Start() {
          research = gameObject.AddComponent<Research>();
          Research();
      }
  
      public void Research() {
          research.Begin();
      }
  }

also i wouldn't recommend calling invoke in the update function because it will invoke that function every frame instead of the specified time. Coroutines are good for that.

Comment
Add comment · 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

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

Convert Component to Actual Type C# to call a coroutine 0 Answers

make events occur only at specific times 1 Answer

I need to Stop and Restart a Co-Routine at anytime 1 Answer

strange behaviour for coroutine and invoke 1 Answer

CancelInvoke cant cancell,Updating InvokeRepeating 1 Answer


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