Search Unity

My Sprite renderer does not change

Discussion in '2D' started by shabbacreates, Mar 4, 2019.

  1. shabbacreates

    shabbacreates

    Joined:
    Jan 9, 2019
    Posts:
    7
    So I have the following code :
    void Start()
    {
    LevelManager = FindObjectOfType<levelManager>();
    soundEffect = FindObjectOfType<SoundEffect>();
    position = transform.position;
    myAnim = GetComponent<Animator>();
    spriteR = GetComponent<SpriteRenderer>();

    }

    public void UpdateCarSprite()
    {
    switch (LevelManager.healthCount)
    {
    case 3:
    return;
    case 2:
    spriteR.sprite = DentedCar;
    return;
    case 1:
    spriteR.sprite = SuperDentedCar;
    return;
    case 0:
    spriteR.sprite = null;
    return;

    default:
    spriteR.sprite = FullCarHealth;
    break;
    }

    }

    The sprite renderer temporarily changes the sprite but by the next frame it is changed back to the original fullcarhealth sprite. I do not understand what is up.
     
  2. Geejayz

    Geejayz

    Joined:
    Feb 8, 2016
    Posts:
    73
    In your code, change the return; keywords to break; instead.
    I'm not certain this is your issue but whatever happens suggests that in the next frame the default value is the choice being made. Is the healthCount becoming something other than given in the options (i.e something other than 0, 1, 2, or 3)?

    Code (CSharp):
    1. switch (LevelManager.healthCount) {
    2.  
    3.     case 3:
    4.     break;
    5.  
    6.     case 2:
    7.     spriteR.sprite = DentedCar;
    8.     break;
    9.  
    10.     case 1:
    11.     spriteR.sprite = SuperDentedCar;
    12.     break;
    13.  
    14.     case 0:
    15.     spriteR.sprite = null;
    16.     break;
    17.  
    18.     default:
    19.     spriteR.sprite = FullCarHealth;
    20.     break;
    21. }