Search Unity

Unity UI Switching Image sprite overtime with CoRoutine causing weird artifacts

Discussion in 'UGUI & TextMesh Pro' started by Dysfunctional-Games, Dec 6, 2018.

  1. Dysfunctional-Games

    Dysfunctional-Games

    Joined:
    Nov 22, 2018
    Posts:
    3
    Hey everybody this is my first post on these forums so please be patient with me if I am doing something against normal routine. I'm working on a game right now and one piece is creating me a headache here. I am trying to at the beginning of the game when it first loads show a series of images for example the company logo and a few other logo's. I'm using an Image UI component and through a CoRoutine I change the image that is displayed after a 5 second delay. It works fine but there is a weird glitch where after it switches from the first image to the second image it shows the second one fine but it occasionally flashes with the first image. Its the same problem when it switches to the next one as well. I'm wondering if anybody has any ideas for me on this one.

    Here is how I'm doing it in code in case this will help figure out the root problem:
    Code (CSharp):
    1. void Start () {
    2.         if (bannerSprites.Length > 0)
    3.         {
    4.             Debug.Log("Called in Start");
    5.             if (bannerImageHolder != null)
    6.             {
    7.                 bannerImageHolder.GetComponent<Image>().sprite = bannerSprites[0];
    8.             }
    9.         }
    10.         else
    11.         {
    12.             SceneManager.LoadScene(1);
    13.         }
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         StartCoroutine(ChangeBanner());
    19.  
    20.         if (doneShowingBanners)
    21.         {
    22.             SceneManager.LoadScene(1);
    23.         }
    24.     }
    25.  
    26.     IEnumerator ChangeBanner()
    27.     {
    28.         for (int bannerCount = 0; bannerCount < bannerSprites.Length; bannerCount++)
    29.         {
    30.             bannerImageHolder.GetComponent<Image>().sprite = bannerSprites[bannerCount];
    31.             yield return new WaitForSeconds(waitTime);
    32.         }
    33.         doneShowingBanners = true;
    34.     }
    Thanks for any help you can all provide me.