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

Sprite disappears when scene is played

Discussion in 'Editor & General Support' started by watchintv, Feb 14, 2020.

  1. watchintv

    watchintv

    Joined:
    Feb 12, 2020
    Posts:
    2
    Hello, I just started learning about Unity and am following some tutorials online to understand the basics.

    I am stuck on this tutorial at https://www.tutorialspoint.com/unity/unity_coroutines.htm

    Basically, I am just trying to get a sprite to change its color by using a coroutine.

    When I play the scene in Unity, the sprite disappears. However, when I look at the Sprite Renderer under Inspector, I can see the sprite's Color changing as expected.

    How can I fix this? I imagine I am missing something simple! Below is the code in my ColorChanger.cs script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ColorChanger : MonoBehaviour
    6. {
    7.     private SpriteRenderer sr;
    8.  
    9.     public Color color1;
    10.     public Color color2;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         sr = GetComponent<SpriteRenderer>();
    16.         StartCoroutine(ChangeColor());
    17.     }
    18.  
    19.     IEnumerator ChangeColor()
    20.     {
    21.  
    22.         while (true)
    23.         {
    24.  
    25.             if (sr.color == color1)
    26.                 sr.color = color2;
    27.  
    28.             else
    29.                 sr.color = color1;
    30.  
    31.             yield return new WaitForSeconds(3);
    32.         }
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.  
    39.     }
    40. }
    41.  
     
  2. watchintv

    watchintv

    Joined:
    Feb 12, 2020
    Posts:
    2
    SOLVED! The Alpha property of the colors I chose was set to 0, rendering the sprite as transparent! Whoops!
     
    NinefiveMedia likes this.