- Home /
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.
Can you create a "Coroutine$$anonymous$$anager" script attached to a gameObject, and call DontDestroyOnLoad
on it?
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.
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.
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.
Answer by jandd661 · Sep 15, 2018 at 12:41 PM
Greetings, If I understand correctly, what needs to happen is:
Player puts a bun in the oven in scene 1 (for example). The bun takes 15 min to cook.
Oven timer starts.
Player goes to scene 2 or 3 or 4 and does really cool stuff.
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.
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);
}
Like seen in the pic, I activated and waited for $$anonymous$$ace1, then activated $$anonymous$$ace2 and 3, $$anonymous$$ace3 got activated twice.
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
Follow this Question
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