- Home /
How to start coroutine from another script?
Did my search and didn't quite find an answer. I'm making a countdown timer. Now - it works very neatly if I use start, update and IEnumerator and attach the entire script to the main camera. But I'd like to be able to have the ability to just have a simple call of the timer in another script. Something like Timer.Start_CountDown();
public static IEnumerator Counter(){
for(int a = 9; a >= 0; a--){
if(a == 9){
for(int x = 0; x <= 9; x++){
Ones_Number[x].GetComponent<SpriteRenderer>().enabled = false;
Tens_Number[x].GetComponent<SpriteRenderer>().enabled = false;
}
Ones_Number[0].GetComponent<SpriteRenderer>().enabled = true;
Tens_Number[9].GetComponent<SpriteRenderer>().enabled = true;
yield return new WaitForSeconds (1f);
}
else
{for(int i = 9; i >= 0; i--){
for(int x = 0; x <= 9; x++){
Ones_Number[x].GetComponent<SpriteRenderer>().enabled = false;
Tens_Number[x].GetComponent<SpriteRenderer>().enabled = false;
}
Ones_Number[i].GetComponent<SpriteRenderer>().enabled = true;
Tens_Number[a].GetComponent<SpriteRenderer>().enabled = true;
yield return new WaitForSeconds (1f);
}
}
}
}
This is the timer base. I tried making a static void which would start the coroutine and that void would be called from another scrip, but it doesn't work.
I'm using c#.
I$$anonymous$$O: $$anonymous$$eep your timer a non-$$anonymous$$onoBehavior class. Give it a constructor, a Time variable, a StartTimer function, and a CountDown coroutine. In any script that you need to use it, just create and save your Timer in that script on Start/Awake, and call StartTimer when you need to. If you want to get really fancy you can add a TimesUp EventHandler to the Timer, and have your controlling script subscribe to it. Good luck with your project.
Answer by zharik86 · Sep 01, 2014 at 06:58 PM
Create two scripts and attach them, for example, at Main Camera. The first script contains your timer with all variables:
using UnityEngine;
using System.Collections;
public class myTimer : MonoBehaviour {
//Create your variable
...
//Your function timer
public IEnumerator Counter() {
...
}
}
In the second script, for example, as Start () we will call the timer:
using UnityEngine;
using System.Collections;
public class myCallTimer : MonoBehaviour {
void Start() {
//myTimer script attach at Main Camera, than
StartCoroutine(Camera.main.GetComponent<myTimer>().Counter());
}
}
I hope that it will help you.
$$anonymous$$eep in $$anonymous$$d that this will run the coroutine on the gameobject where the "myCallTimer" script is attached to, not on the main camera. As long as the gameobject and the script aren't destroyed there's no problem, it just can lead to problems in cases where the calling script / object is destroyed.
If you want to run the coroutine on the "myTimer" script you should do:
void Start() {
myTimer obj = Camera.main.GetComponent<myTimer>();
obj.StartCoroutine(obj.Counter());
}
ps: Classnames (in almost any program$$anonymous$$g languages) should start with an upper case letter.
StartCoroutine(Camera.main.GetComponent<myTimer>().Counter());
That was it! Thank YOU :)
Your answer
Follow this Question
Related Questions
Static Coroutine being called endlessly 2 Answers
How can I instantiate a prefab from a static function? 1 Answer
Best way to make melee AI damage player? 2 Answers
"Object reference required" error when using Start/StopCoroutine 1 Answer
Singleton instance accessed in coroutine always null 1 Answer