Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved]Coroutine Issue

Discussion in 'Scripting' started by wittledemon, Jun 17, 2019.

  1. wittledemon

    wittledemon

    Joined:
    Mar 27, 2017
    Posts:
    16
    I have a coroutine attached to a prefab but when I run the game only the last instance of the prefab plays the coroutine. Can someone explain why this is?
    Code (CSharp):
    1. IEnumerator AnimateBox()
    2. {
    3.           for (int counter = 0; counter <= sprite.Length; counter++)
    4.           {
    5.                     animateSprite.sprite = sprite[counter];
    6.                     yield return new WaitForSeconds(animateSpeed);
    7.                     if (counter == 3)
    8.                    {
    9.                              counter = -1;
    10.                    }
    11.           }    
    12. }
     
  2. Kwinten

    Kwinten

    Joined:
    Jan 25, 2015
    Posts:
    49
    Nothing about the coroutine itself could help us identify the problem.

    We need to see the code that starts the coroutine.
     
  3. wittledemon

    wittledemon

    Joined:
    Mar 27, 2017
    Posts:
    16
    Code (CSharp):
    1.  void Start()
    2. {
    3.           animateSprite = SpriteRenderer.FindObjectOfType<SpriteRenderer>();
    4.           StartCoroutine(animateBox);
    5. }
     
  4. Helical

    Helical

    Joined:
    Mar 2, 2014
    Posts:
    50
    I am guessing that all your prefabs have scripts that reference (link, connect, point to) the same "animateSprite".

    Perform the following check

    Make sure each Prefab instance has a different name and Debug.Log(name) each time AnimateBox is called, to make sure they are called for each prefab instance.

    Also check that each prefab references its own animateSprite and not someone elses. You can do this by clicking on that serialized field in inspector for each prefab.
     
  5. Helical

    Helical

    Joined:
    Mar 2, 2014
    Posts:
    50

    If there are multiple SpriteRenderer in your scene, FindObjectOfType<>() will always find the same one. You want to link up each prefab with its own SpriteRenderer.

    Try addign a serialized field

    [SerializeField] SpriteRenderer _myRenderer;

    Connecting via Inspector by drag and droping.
     
    Kwinten likes this.
  6. Kwinten

    Kwinten

    Joined:
    Jan 25, 2015
    Posts:
    49
    @Helical is completely correct, this is definitely your issue.

    In general, try to avoid ever using any of the
    Find...()
    functions.
     
  7. wittledemon

    wittledemon

    Joined:
    Mar 27, 2017
    Posts:
    16
    I had done the Debug.Log(name) and all are being called when the coroutine starts up for each one. But the second part I do not quite understand I have the array serialized so that i can change the pics for each and the size of the array. If this is what you mean then yes I have done this.
     
  8. Kwinten

    Kwinten

    Joined:
    Jan 25, 2015
    Posts:
    49
    This is the problematic line of code:

    Code (CSharp):
    1. animateSprite = SpriteRenderer.FindObjectOfType<SpriteRenderer>();
    All your instances are spawned, they all find the exact same single SpriteRenderer, and then each instance changes the sprite of that one single SpriteRenderer. Presumably you want each object to reference their own SpriteRenderer, which you would do through a serialized field.
     
  9. wittledemon

    wittledemon

    Joined:
    Mar 27, 2017
    Posts:
    16
    I fixed the issue by changing it to a get component but I would like to know why it is not good to use find like you stated in your first post. If you would explain why that be awesome please?
     
  10. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    FindObjectOfType will search the entire scene for that type of object and return the first one it finds. If you have more than one SpriteRenderer in the scene (by having many objects, most likely), then FindObjectOfType will always return the same one. In your case, suppose each of your sprite objects were named "Sprite0", "Sprite1", "Sprite2" etc. Sprite0 would find its own renderer, but Sprite1 would also find Sprite0's renderer, and Sprite99 would also find Sprite0's renderer. So, later when you are trying to animate, they all animate Sprite0's renderer, not their own.
     
  11. wittledemon

    wittledemon

    Joined:
    Mar 27, 2017
    Posts:
    16
    Thank you very much!