Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Testing Sprite Image

Discussion in 'Scripting' started by JLoundy, Feb 28, 2020.

  1. JLoundy

    JLoundy

    Joined:
    Feb 28, 2020
    Posts:
    4
    Hey everybody,

    I'm working on a project where when I click a button a separate images sprite it changed. I'm wondering how to properly set up the if statements so it checks which sprite is currently being displayed that way I can change the sprite to the next in line. I just have no clue how to properly test this.

    Any help would be much appreciated :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WoodState : MonoBehaviour
    6. {
    7.     public Sprite Wood_0, Wood_1, Wood_2, Wood_3;
    8.    
    9.    
    10.     public void NextWoodState()
    11.     {
    12.         if (GetComponent<Sprite>() == Wood_0)
    13.         {
    14.             GetComponent<SpriteRenderer>().sprite = Wood_1;
    15.             Debug.Log("Next state 0");
    16.         }
    17.  
    18.         if (GetComponent<Sprite>() == Wood_1)
    19.         {
    20.             GetComponent<SpriteRenderer>().sprite = Wood_2;
    21.             Debug.Log("Next state 1");
    22.         }
    23.  
    24.         if (GetComponent<Sprite>() == Wood_2)
    25.         {
    26.             GetComponent<SpriteRenderer>().sprite = Wood_3;
    27.             Debug.Log("Next state 2");
    28.         }
    29.  
    30.         if (GetComponent<Sprite>() == Wood_3)
    31.         {
    32.             GetComponent<SpriteRenderer>().sprite = Wood_0;
    33.             Debug.Log("Next state 3");
    34.         }
    35.  
    36.     }
    37.  
    38. }
     
  2. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Code runs from top to bottom, so in your case here, you'll go into NextWoodState() and do the first if statement. Assuming the first sprite is Wood_0, it'll change to Wood_1 and then go to the next if statement... which is also true, and so on.

    All you'll see in the scene is Wood_3 straight away, but it would have gone through everything.

    A quick way to change this is to have else if for Wood_1 onwards, then once a condition has been met the code exits the method. (this isn't the best way)

    Also, cache the sprite you're checking against, don't do GetComponent so often like that.

    Code (csharp):
    1. SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
    2.  
    3. if (spriteRenderer.sprite == Wood_0)
    4. {
    5. spriteRenderer.sprite = Wood_1;
    6. }
    7. else if (spriteRenderer.sprite == Wood_1)
    8. {
    9. ...
    10. }
    11.  
     
    JLoundy likes this.
  3. JLoundy

    JLoundy

    Joined:
    Feb 28, 2020
    Posts:
    4
    Ahh I see, thanks for the help and explanation. I got it working now!