Hi everyone, I have an issue using Coroutines. Basically my problem is that I made a button prefab, with a fade effect when clicked. Now, I obviously made a script for this, two scripts actually, the one responsible for the instantiate of the prefab and controlling of events like mouse click etc. And another one, which is a controller, that stops and calls a coroutine when a certain condition is met, a manager of animations.
Now, my issue is that if I instance another prefab of my button, and I click both of them, the first coroutine gets locked in the state it is, because the one on the other button has to start. How do I avoid this? Can I just say that the coroutine has to end if the call is made from somewhere else?
I include the part of the script that manages the coroutine.
public function Fading(colorA : Color, colorB : Color, go : GameObject, animation_time : float)
{
var parameters : Dictionary.<String,Object> = new Dictionary.<String,Object>();
parameters.Add("ColorA", colorA);
parameters.Add("ColorB", colorB);
parameters.Add("GameObject", go);
parameters.Add("AnimationTime", animation_time);
StopCoroutine("FadeToColor");
StartCoroutine("FadeToColor", parameters);
}
This script is part of my manager script, it gets called using a Singleton.
Thanks in advance
Hey,
if you use the named Coroutine ( if u start it using a string) you can always only have one instance.
You can use StartCoroutine(FadeToColor(parameters));
. But if you want to stop it again, you need a reference to it:
IEnumerator coroutine = FadeToColor(parameters);
StartCoroutine(coroutine);
StopCoroutine(coroutine);
See: Unity - Scripting API: MonoBehaviour.StopCoroutine
1 Like
StartCoroutine gives you the coroutine back now. So you can just do this
Coroutine cor = StartCoroutine(FadeToColor(parameters));
StopCoroutine(cor);
1 Like
Thanks for your answers, I had to use the string call method because, even though I was able to call the coroutine with the routine name, I’m not able to stop it. If I do as suggested above StopCoroutine is not getting called, I can comment it and the coroutine starts, gets to conclusion, but doesn’t actually stop when needed.
I.E. I have a button that has to fade OnPointerDown and Up, so when you press on it, it should fade to a color, as soon as you release it, it should fade back. If I call it like that, when I release, it just goes back, without actually fading.
In the end I had to make use of lists to get around the problem
public function AnimationHelper()
{
animation_list = new Dictionary.<String,GameObject>();
}
public function appendAnimationHelper(go : GameObject,animation_name : String) : AnimationHelper
{
if(!animation_list.ContainsKey(go.GetInstanceID()+animation_name))
{
go.AddComponent.<AnimationHelper>();
animation_list.Add(go.GetInstanceID()+animation_name, go);
}
return go.GetComponent.<AnimationHelper>();
}
Then in my call function
public function Fading(colorB : Color, go : GameObject, animation_time : float, animation_name : String)
{
if(gameObject.name==goName)
{
appendAnimationHelper(go,animation_name).Fading(colorB,go,animation_time,animation_name);
}
else
{
var parameters : Dictionary.<String,Object> = new Dictionary.<String,Object>();
parameters.Add("ColorB", colorB);
parameters.Add("GameObject", go);
parameters.Add("AnimationTime", animation_time);
StopCoroutine("FadeToColor");
StartCoroutine("FadeToColor", parameters);
}
}