- Home /
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()
.
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.?
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
.
If you do a Debug.Log("start") in your Begin(), do you get this logged.?
Yes, I assure you that the issue is not that the method isn't called.
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...???
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.