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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Moving a set of pictures in an array in two directions

Discussion in 'Scripting' started by herman111, May 29, 2016.

  1. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    I'm trying to move some pictures in an array forward with one UI Button then reverse with another button.I have it working somewhat, but when I reverse the numbers don't work right...if going forward to the last one..if its the number 4 in the array...hitting the reverse button , should bring up picture number 4...instead it starts with 1,then 0, then -0..here's my script
    Code (CSharp):
    1.  
    2. // cycle pictures forward and reverse
    3. // using Unity 4.6
    4.  
    5. using UnityEngine;
    6. using System.Collections;
    7. using UnityEngine.UI;
    8.  
    9. public class Back_Forth : MonoBehaviour
    10. {
    11.  
    12.     public Sprite[] fish;
    13.     //public TextAsset[] myAssets;
    14.     public static int FWcount = 0;
    15.     public Image FishImage;
    16.     public Button forward;
    17.     public Button back;
    18.     //public Text myText;
    19.  
    20.     void Start ()
    21.     {
    22.         FishImage.sprite= fish[FWcount];
    23.         //myText.text=""+myAssets[FWcount];
    24.     }
    25.    
    26.     public void ForwardFish ()
    27.     {
    28.         if(forward)
    29.         {
    30.             FWcount  = FWcount +1;
    31.             FishImage.sprite= fish[FWcount];
    32.             //myText.text=""+myAssets[FWcount];
    33.             if(FWcount>=2)
    34.             {   FWcount = 2;  }
    35.  
    36.         }
    37.     }
    38.  
    39.     public void BackFish ()
    40.     {
    41.         if(back && FWcount>=0)
    42.         {
    43.             FishImage.sprite= fish[FWcount];
    44.             //myText.text=""+myAssets[FWcount];
    45.             FWcount  = FWcount -1;
    46.             if(FWcount<0)
    47.             {   FWcount = 0;  }
    48.         }
    49.     }
    50.    
    51. }
    52.  
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    You need to set the sprite after you calculate the number. Change it to:
    Code (csharp):
    1.  
    2. public void ForwardFish ()
    3.     {
    4.         if(forward)
    5.         {
    6.             FWcount  = FWcount +1;
    7.             //myText.text=""+myAssets[FWcount];
    8.             if(FWcount>=2)
    9.             {   FWcount = 2;  }
    10.              FishImage.sprite= fish[FWcount];
    11.         }
    12.     }
    13.  
    14.     public void BackFish ()
    15.     {
    16.         if(back && FWcount>=0)
    17.         {
    18.             //myText.text=""+myAssets[FWcount];
    19.             FWcount  = FWcount -1;
    20.             if(FWcount<0)
    21.             {   FWcount = 0;  }
    22.             FishImage.sprite= fish[FWcount];
    23.         }
    24.     }
     
  3. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    Thanks a lot..I guess order is everything