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. Dismiss Notice

how do I Subtract an array element from an array once an interger decreases

Discussion in 'Scripting' started by xxfmxx, Feb 23, 2015.

  1. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    basically im creating a health bar basically like zelda and im pretty sure theres another way of doing this but this is what I came up with on my own..essentially theres an array holding a number of sprites and a few if statements changing the public in health variable to decrease by 1 what i want to do is to either disable the sprite render as the health decreases or to simply subtract the element all together and then when the level resets for the array elements to come back

    heres the code im working with
    please keep any sample codes in c#
    also project is in 2d shovel knight, mega man , mario etc
    a little FYI i have read the api and various resources on for loops and such
    but my brain is still not working I kinda get what to do and kinda dont at the same time


    Code (CSharp):
    1. public class CharacterHealth : MonoBehaviour {
    2.  
    3.     public Texture2D[] mSprites;
    4.     public float health = 5.0f;
    5.     private float timer;
    6.     private float waitTimer = 2.0f;
    7.     private Move characterMovement;
    8.     private Respawn respawn;
    9.    
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Awake ()
    14.     {
    15.  
    16.         characterMovement = GetComponent<Move>();
    17.        
    18.  
    19.            
    20.     }
    21.  
    22.     void OnCollisionEnter2D (Collision2D other)
    23.     {
    24.         if (other.gameObject.tag == "Damage")
    25.         {
    26.             health = health - 1;
    27.  
    28.            
    29.  
    30.        
    31.         }
    32.     }
    33.  
    34.  
    35.     // Update is called once per frame
    36.     void Update ()
    37.  
    38.     {
    39.  
    40.         for(int i = 0; i < mSprites.Length; i++)
    41.         {
    42.             //would check how many sprites there are
    43.             //and subtract the appropiate sprite according
    44.             //to how much health is left
    45.  
    46.         }
    47.  
    48.         if(health == 5.0f)
    49.         {
    50.             print ("health is 5");
    51.  
    52.            
    53.         }
    54.        
    55.         if(health == 4.0f)
    56.         {
    57.             print ("health is 4");
    58.             //array length 4 would dissaper
    59.            
    60.         }
    61.         if(health == 3.0f)
    62.         {
    63.             print ("health is 3");
    64.             //array length 3 would dissaper
    65.            
    66.         }
    67.         if(health == 2.0f)
    68.         {
    69.             print ("health is 2");
    70.             //array length 2 would dissaper
    71.            
    72.         }
    73.         if(health == 1.0f)
    74.         {
    75.             print ("health is 1");
    76.             //array length 1 would dissaper
    77.         }
    78.         if(health <= 0)
    79.         {
    80.             print ("health is 0");
    81.             characterMovement.enabled = false;
    82.            
    83.             ResetLevel ();
    84.         }
    85.     }
    86. void ResetLevel()
    87.         {  
    88.         timer += Time.deltaTime;
    89.         if(timer >= waitTimer)
    90.         {
    91.             Application.LoadLevel(0);
    92.             print ("Restart");
    93.         }
    94.         }
    95. }
     
  2. Buddy_Redmond

    Buddy_Redmond

    Joined:
    Aug 30, 2014
    Posts:
    23
    There are many ways to do this. The simplest, I think, would be:
    Code (CSharp):
    1.  
    2. void Update() {
    3.     for (int i = 0; i < mSprites.Length; i++) {
    4.         if (i + 1 > health) {
    5.             mSprites[i].enabled = false;
    6.         }
    7.     }
    8. }
    9.  
     
  3. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    building off of @Buddy_Redmond 's solution, a more efficient way would be to put that for loop in a separate method and call it only when your character takes damage so it's not checking every frame.
     
  4. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    enabled never worked for me when i used it what i ended up
    doing was using a SetActive(false) instead of a for loop
    Code (CSharp):
    1.    
    2.  if(health == 1.0f)
    3.         {
    4.             mSprites[1].SetActive(false);
    5.  
    6.             Debug.Log ("health is 1");
    7.             //array length 1 would dissaper
    8.         }
    9.         if(health <= 0)
    10.         {
    11.             mSprites[0].SetActive(false);
    12.             Debug.Log ("health is 0");
    13.             characterMovement.enabled = false;
    14.            
    15.             ResetLevel ();
    16.         }
     
  5. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    The problem here is that this only works for health values of 1 and zero and it's messy to have an if statement for every possible health value. That's why Buddy_Redmond suggested a for loop. Doesn't matter if it's .enabled or .setActive. Same principle.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,183
    OP: you cannot "subtract an array element from an array". An array has a fixed size. Once you have made it, it's size cannot change.
     
    Deleted User likes this.
  7. Buddy_Redmond

    Buddy_Redmond

    Joined:
    Aug 30, 2014
    Posts:
    23
    True, but you can hide/ignore some elements to make it appear as if it has changed size. The player won't care if the health is tracked with an array, list, or some other data structure, what matters is that the functionality is there and it works for the program and programmer.
     
    TheValar likes this.
  8. Deleted User

    Deleted User

    Guest

    Then why not make it a list in the first place?
     
  9. Buddy_Redmond

    Buddy_Redmond

    Joined:
    Aug 30, 2014
    Posts:
    23
    It depends on what you want to do. For this, he has a few things he wants to track and sometimes hide. At such a small scale the performance differences wouldn't really matter. An example is if he wants to make it so you can get more and more health then a list would work way better. Otherwise, the difference is just between hiding the object for an array and deleting it for a list. An example for using the array would be, maybe he wants to modify the texture of the depleted health. An array tracks all of the health, depleted or not, but a list would delete them which would make it so you cannot access the depleted health.
     
  10. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    ahh yeah that make sense i suppose,,,,ima try that way too with set active