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

Question Changing Sprites Not Working

Discussion in 'Scripting' started by xkrbl, Feb 3, 2023.

  1. xkrbl

    xkrbl

    Joined:
    Aug 28, 2018
    Posts:
    10
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyHealthBar : MonoBehaviour
    6. {
    7.     public float hp;
    8.     public Sprite es_0, es_1, es_2, es_3, es_4, es_5, es_6, es_7, es_8, es_9, es_10, es_11, es_12, es_13;
    9.  
    10.     void Start()
    11.     {
    12.         hp = 100;
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if (hp >= 93)
    18.         {
    19.             GetComponent<SpriteRenderer>().sprite = es_0;
    20.         }
    21.         if (hp <= 92 && hp >= 86)
    22.         {
    23.             GetComponent<SpriteRenderer>().sprite = es_1;
    24.         }
    25.         if (hp <= 85 && hp >= 79)
    26.         {
    27.             GetComponent<SpriteRenderer>().sprite = es_2;
    28.         }
    29.         if (hp <= 78 && hp >= 71)
    30.         {
    31.             GetComponent<SpriteRenderer>().sprite = es_3;
    32.         }
    33.         if (hp <= 70 && hp >= 63)
    34.         {
    35.             GetComponent<SpriteRenderer>().sprite = es_4;
    36.         }
    37.         if (hp <= 62 && hp >= 55)
    38.         {
    39.             GetComponent<SpriteRenderer>().sprite = es_5;
    40.         }
    41.         if (hp <= 54 && hp >= 47)
    42.         {
    43.             GetComponent<SpriteRenderer>().sprite = es_6;
    44.         }
    45.         if (hp <= 46 && hp >= 39)
    46.         {
    47.             GetComponent<SpriteRenderer>().sprite = es_7;
    48.         }
    49.         if (hp <= 38 && hp >= 31)
    50.         {
    51.             GetComponent<SpriteRenderer>().sprite = es_8;
    52.         }
    53.         if (hp <= 30 && hp >= 23)
    54.         {
    55.             GetComponent<SpriteRenderer>().sprite = es_9;
    56.         }
    57.         if (hp <= 22 && hp >= 15)
    58.         {
    59.             GetComponent<SpriteRenderer>().sprite = es_10;
    60.         }
    61.         if (hp <= 14 && hp >= 7)
    62.         {
    63.             GetComponent<SpriteRenderer>().sprite = es_11;
    64.         }
    65.         if (hp <= 6 && hp >= 4)
    66.         {
    67.             GetComponent<SpriteRenderer>().sprite = es_12;
    68.         }
    69.         if (hp <= 3 && hp > 0)
    70.         {
    71.             GetComponent<SpriteRenderer>().sprite = es_10;
    72.         }
    73.         if (hp <= 0)
    74.         {
    75.             Death();
    76.         }
    77.     }
     
  2. xkrbl

    xkrbl

    Joined:
    Aug 28, 2018
    Posts:
    10
    Never mind, it worked. But can anyone tell me how to make my code cleaner and simpler? :p
     
  3. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    For one: Move the GetComponent<>-call to Start or Awake, and only call it once (save the result to a variable).

    A second thing you can do is to create a struct:
    Code (CSharp):
    1. [System.Serializable]
    2. public struct ThresholdSprite
    3. {
    4.     public float Threshold;
    5.     public Sprite Sprite;
    6. }
    You can then set those in the Inspector:

    public ThresholdSprite[] Sprites;


    and then, in Update, you can sort the Array by the Threshold(s), and loop through them.
    The first one you find that matches your current HP(-range) can then be used to set the Sprite.
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    So an example would be:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [RequireComponent(typeof(SpriteRenderer)]
    5. public class EnemyHealthBar : MonoBehaviour
    6. {
    7.     [System.Serializable]
    8.     public struct ThresholdSprite
    9.     {
    10.         public float Threshold;
    11.         public Sprite Sprite;
    12.     }
    13.  
    14.     public float hp;
    15.     public ThresholdSprite[] Sprites;
    16.    
    17.     private SpriteRenderer sprRenderer;
    18.    
    19.     void Awake()
    20.     {
    21.         sprRenderer = GetComponent<SpriteRenderer>();
    22.     }
    23.    
    24.     void Start()
    25.     {
    26.         hp = 100;
    27.     }
    28.     void Update()
    29.     {
    30.         if (IsDead)
    31.             return;
    32.            
    33.         if (hp <= 0)
    34.         {
    35.             Die();
    36.             return;
    37.         }
    38.    
    39.         for (int i = 0; i < Sprites.Length; i++)
    40.         {
    41.             if (hp >= Sprites[i].Threshold)
    42.             {
    43.                 sprRenderer.sprite = Sprites[i].Sprite;
    44.                 break;
    45.             }
    46.         }
    47.     }
    48.    
    49.     // OnValidate runs in the Editor, when Inspector-Changes are applied.
    50.     // This allows you to validate any options that were set
    51.     void OnValidate()
    52.     {
    53.         if (Sprites != null && Sprites.Length > 0)
    54.             Sprites = Sprites.OrderBy(s => s.Threshold).ToArray();
    55.     }
    56. }
    Of course, there are still cleaner ways to do it (using Events, pub/sub, etc.).
    But this is a start.