Same Coroutine on multiple objects
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, one is the one responsible for the instantiate of the prefab and controlling of events like mouse click etc. The other one is a controller, that stops and calls a coroutine when a certain condition is met.
Now, my issue is that if I instance another prefab of my button, and I click both of them, the first coroutine gets stopped, 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 manage 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);
}
I also would like to clarify that the controller uses a Singleton. Thanks in advance.
Answer by davide-aldegheri · May 04, 2016 at 10:18 AM
In the end I had to make use of lists to use this properly
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);
}
}