Search Unity

Using a Sprite Renderer Array to change sprites up and down (in order)

Discussion in 'Scripting' started by Plott, Oct 24, 2014.

  1. Plott

    Plott

    Joined:
    May 12, 2014
    Posts:
    94
    Im super lost in trying to figure out how to do this correctly, I have written out i THINK is what is close to the correct code, but I'm lost now.

    Code (CSharp):
    1.     public Sprite[] array;
    2.  
    3.     int imageNumber = 0;  
    4.  
    5. void Start ()
    6.     {
    7.         GetComponent<SpriteRenderer> ().sprite = array [49];
    8.     }
    9.  
    10. public void BatteryCollected (Vector2 contactPoint)
    11.     {
    12.    
    13.         SpriteRenderer = array[imageNumber];
    14.            
    15.         if(imageNumber == array.Count - 1)
    16.         {
    17.             imageNumber = 0;
    18.         }
    19.         else
    20.         {
    21.             imageNumber++;
    22.         }          
    23.     }
    So its not really working at all, but what i want to do is:

    If you collect a battery in my game, then the image of battery power on screen will move in positive order of sprites array.

    if you move the player it goes in negative order of the sprite array.

    Any idea of what I'm writing wrong? I'm confused at where to with this, especially since array.Count doesn't work.
     
    robomarti likes this.
  2. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    array is a type of Sprite[] which is not a List. So to get the number of elements you have to use array.Length
     
    robomarti likes this.
  3. Plott

    Plott

    Joined:
    May 12, 2014
    Posts:
    94
    Ok so I tried to get this to work, but its not moving changing the sprite sequence. I think my issue is on this code

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         GetComponent<SpriteRenderer> ().sprite = array[25];
    4.     }
    I only put 25 there to prove that is was moving to that number in the sequence when clicked and it is. But it needs to be something different. array.Length didn't work there, or at least it would except it, just gave me an error.

    Here is the code i changed. I could really use some knowledge on what to do differently, I have a hard time deciphering Unity Documentation

    this is where i add or subtracted from a different script.
    Code (CSharp):
    1.  
    2.     public void BatteryCollected (Vector2 contactPoint)
    3.     {
    4.         Debug.Log ("Battery Plus");
    5.  
    6.         batteryG.BatteryPlus();
    7.  
    8.     }
    9.  
    10.     public void BatteryMinus ()
    11.     {
    12.         Debug.Log ("Battery Minus");
    13.      
    14.         batteryG.BatteryMinus();
    15.      
    16.     }
    So then they talk to this script

    Code (CSharp):
    1. public class batteryGUI : MonoBehaviour {
    2.  
    3.     public Sprite[] array;
    4.  
    5.     int imageNumber = 0;
    6.  
    7.     void Start()
    8.     {
    9.         GetComponent<SpriteRenderer> ().sprite = array[25];
    10.     }
    11.  
    12.  
    13.     public void BatteryPlus()
    14.     {
    15.  
    16.  
    17.         if(imageNumber == array.Length - 1)
    18.         {
    19.             imageNumber = 0;
    20.         }
    21.         else
    22.         {
    23.             imageNumber++;
    24.         }
    25.  
    26.  
    27.  
    28.     }
    29.  
    30.     public void BatteryMinus()
    31.     {      
    32.          
    33.          if(imageNumber == 0)
    34.         {
    35.             imageNumber = array.Length - 1;
    36.         }
    37.         else
    38.         {
    39.             imageNumber--;
    40.         }
    41.  
    42.     }
    43.  
    44. }
     
    robomarti likes this.
  4. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    The fact is that you're substracting your imageNumber, but you're not reading your array with it. Add this at the end of your BatteryMinus and BatteryPlus func:

    GetComponent<SpriteRenderer>().sprite= array[imageNumber];
     
    robomarti likes this.
  5. Plott

    Plott

    Joined:
    May 12, 2014
    Posts:
    94

    I'm so stupid. It works now, thanks for your help!
     
    robomarti likes this.