Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Torqus · Sep 15, 2018 at 11:44 AM · coroutinestaticinstancescenesmanager

Instantiating a Coroutine?

I'm super tired of trying to do this and with no avail.
What I simply need to do is make a timer that persist through scenes, and I'm super stuck, I've tried so many things I can't remember them all.
I have a GameManager that persist through scenes, so the script has to run there, and when I click or activate something that has a timer, a new instance of the "Timer" appears, that would be a coroutine with references to the given TimeToFinish and the GameObject that created it (so i can do its action when it finishes).
The problem is I can't make static coroutines, and I can't send parameters to a method that isn't static, so what can I do? I ended up in a circle of trying something, failing, trying something different, failing again.

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 Hellium · Sep 15, 2018 at 11:49 AM 1
Share

Can you create a "Coroutine$$anonymous$$anager" script attached to a gameObject, and call DontDestroyOnLoad on it?

avatar image Torqus Hellium · Sep 15, 2018 at 12:15 PM 0
Share

Yes, I already have that, I have a G$$anonymous$$ and I add a script where I try to instantiate a coroutine, the problem is I need the script to instantiate the coroutine so I can have multiple timers through scenes.
Think about it like: player goes to forest, start task to pick wood (5$$anonymous$$), start task to pick stone (10$$anonymous$$), player goes to other scene, player comes back(8$$anonymous$$ passed), player has wood but not stone.
I made the events and timers that instantiate and work independant from each other on the same scene, but I can't make them persist through scenes because Instantiating + Coroutines + Statics don't work together.

avatar image Hellium Torqus · Sep 15, 2018 at 01:55 PM 1
Share

What I don't understand is your problem with the static functions. Why do you need them ? In order to access them from other scripts ? If so, that's a very bad idea. Static members are not meant to be used in this case. You should have non static functions and retrieve a reference of your game $$anonymous$$anager in some way : direct reference in the inspector / FindObjectOfType, or even the singleton design pattern.

2 Replies

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

Answer by Torqus · Sep 16, 2018 at 04:58 AM

I was able to make this work, turns out I was making it super complicated and all I had to do was spawn a prefab in GameManager for each timer, i didn't need statics, and now fully understand how they work. Thanks Hellium for your answers I only wish I knew how to use this site before so I could see your answer. Came here to close this thread and saw what I had just tested.

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
avatar image
0

Answer by jandd661 · Sep 15, 2018 at 12:41 PM

Greetings, If I understand correctly, what needs to happen is:

  1. Player puts a bun in the oven in scene 1 (for example). The bun takes 15 min to cook.

  2. Oven timer starts.

  3. Player goes to scene 2 or 3 or 4 and does really cool stuff.

  4. In the current scene(2, 3 or wherever), the player is notified that the bun in the oven of scene 1 is ready.

Is that the gist of it? If it is, something else to consider is; "does the oven state need to be saved between sessions?" Because that would change how you do this.

I know it sucks to have a question answered by a question but, It will help if I and others have a clearer and complete understanding of the end goal.

Comment
Add comment · Show 2 · 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 Torqus · Sep 15, 2018 at 10:41 PM 0
Share

Yes, something like that. I'm kinda getting it to work by doing something I hadn't tested before, I did a prefab that finds the Game$$anonymous$$anager and spawns as a child of it, this prefab has the timer coroutine and it works through scenes, after the time ends, a bool triggers in the manager child, the problem I have now is that when a new timer is instantiated, the old timer triggers the new one and then the new one triggers itself, the old timer actions aren't played, I still can't have 2 timers at once.

 StaticTimer.StartCoroutine(thisObject, originalScene, myTimer, timer);
     
 ///////////////////////////////////////////////////////////////////////////////////////
     
 public class StaticTimer : $$anonymous$$onoBehaviour
 {
     public bool timerEnded = false;
     public string originalObj;
     public string originalScene;
 
     static GameObject manager;
     static GameObject thisTimer;
     static StaticTimer staticTimer;
 
     public static void StartCoroutine(GameObject originalObject, string originalScene, CircularTimer myTimer, float time)
     {
 
         manager = GameObject.FindGameObjectWithTag("G$$anonymous$$");
         thisTimer = Resources.Load<GameObject>("Prefabs/Timer");
         thisTimer = Instantiate(thisTimer);
         staticTimer = thisTimer.GetComponent<StaticTimer>();
         thisTimer.transform.SetParent(manager.transform, false);
         staticTimer.originalScene = originalScene;
         thisTimer.GetComponent<StaticTimer>().StartCoroutine($$anonymous$$yCoroutine(originalObject, time));
     }
 
     static IEnumerator $$anonymous$$yCoroutine(GameObject obj, float time)
     {
         obj.GetComponent<Timer>().TimerStart.Invoke();
         staticTimer.originalObj = obj.name;
         Debug.Log(staticTimer.originalObj);
         yield return new WaitForSeconds(time);
         TimerEnded();
     }
 
     public static void TimerEnded()
     {
         staticTimer.timerEnded = true;
         $$anonymous$$essageSystem.TimerEnded(staticTimer.originalObj);
     }
 
 

alt text

Like seen in the pic, I activated and waited for $$anonymous$$ace1, then activated $$anonymous$$ace2 and 3, $$anonymous$$ace3 got activated twice.

avatar image Torqus Torqus · Sep 15, 2018 at 11:22 PM 0
Share

Also the Object stat would need to be saved through game sessions, I was waiting to deal with this later but it might really change the way I do this.

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

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Singleton instance accessed in coroutine always null 1 Answer

Create an Instance? 2 Answers

"Object reference required" error when using Start/StopCoroutine 1 Answer

Coroutine cannot be automatically started from a static function 1 Answer

What the best way to initialize constant static data in a MonoBehavior? 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