Array reference being changed without me knowing, or, When a coroutine is run on a monobehavior and then stopped with StopCoroutine (coroutine) it stops the same coroutine being run on another instance of the same monobehavior on a different object.
[Edit] Turned out to be an issue with an array reference appearing to be changed to a different one, when it was actually the same array still having references passed around.
I am starting the coroutines separately on each gameobject and the code within the coroutine is supposed to be stopping the coroutine stored in a field in that instance of the monobehavior (itself).
I know this is happening because i am debug logging the name of the gameobject from within the coroutine and it is correctly showing two running at once from different objects, but then both coroutines stop when one is supposed to, leaving the first coroutine unfinished.
is this intended behavior? do i really have to somehow manage an array of coroutine references to have the same one run multiple times simultaneously?
Just tried storing the coroutine like coroutine = StartCoroutine (Coroutine ()) and then stopping coroutine. still stops them both??? Why?
Answer by musashi255 · Dec 07, 2017 at 03:56 AM
[actual answer] It was a problem with array references being passed around and never cleared. i was using the same array that other classes had references to when i though i was passing a different array. Not the first time i did this to myself : | I will try to adjust the question and tags so it will be found by others having the same problem but not regarding coroutines.
[2nd edit] it is starting to look like this is an issue with a class i wrote to hold data, which is not serializable. in this class i have an array of references to the same type of class which is used temporarily to iterate through and do things. it seems when i change the array for a new one, that other instances of that class are also changed. I am checking this class and the array within it throughout the process of the coroutine, and although the reference to the class does not change, the array within the class is changing even though... wait ................ ok while writing this i figured it out and fixed it. answer above.
[Edit] No, actually the below wasn't true, good thing i didn't accept that answer! going to resort to not storing the coroutine at all and creating a new one each call like StartCoroutine (Coroutine ()), and if performance is an issue which i doubt in this case, i will have to have an array of IEnumerators to start and stop separately.... very inconvenient, i would think each mono should start and stop it's own coroutines independently.
[original incorrect answer]Turns out I was setting the reference to the wrong object and thus the monos that were supposed to still have coroutines running on them were simply no longer the target for that coroutine.
Your answer
Follow this Question
Related Questions
Trouble with coroutine 0 Answers
Coroutine Not working properly. 1 Answer
c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers
Need help using coroutines 1 Answer