- Home /
One button doesn't cycle through an array when pressed.
I have an odd problem where my "Next" button works fine and cycles through the array. But my "Back" button doesn't do that properly. I basically want my Back button to work the same as my Next button but in reverse. (The Next button uses arrayVariable++; to cycle through the array while Back uses arrayVariable--;)
I already did some debugging with the Back button and it seems like both are separate and not using the same variable (if that makes sense), but they have the same scripts attached to both of the buttons.
For example, clicking the Next button will increase the arrayVariable by 1, and lets say I increase it to 5 or 6. Clicking the Back button won't decrease but instead start at 0 and decrease from there.
[CODE]
using UnityEngine;
using UnityEngine.UI;
public class ButtonClicky : MonoBehaviour {
public Text TextLabelGO;
public Text QuoteNumber;
public Text LevelCurrent;
public Text LevelExp;
private string[] dialogStrings = new string[11];
public int currentLineNumber = 0;
private int quoteMaxNumber = 10;
private int level = 1;
private int expCurrent = 0;
private int expMax = 5;
private bool reachedQuoteMax = false;
void Start()
{
dialogStrings[0] = "Sentence 0";
dialogStrings[1] = "Sentence 1";
dialogStrings[2] = "Sentence 2";
dialogStrings[3] = "Sentence 3";
dialogStrings[4] = "Sentence 4";
dialogStrings[5] = "Sentence 5";
dialogStrings[6] = "Sentence 6";
dialogStrings[7] = "Sentence 7";
dialogStrings[8] = "Sentence 8";
dialogStrings[9] = "Sentence 9";
dialogStrings[10] = "Sentence 10";
TextLabelGO.text = dialogStrings[currentLineNumber];
QuoteNumber.text = "Quote " + ((int)currentLineNumber).ToString() + " of " + ((int)quoteMaxNumber).ToString();
LevelCurrent.text = ((int)level).ToString();
LevelExp.text = ((int)expCurrent).ToString("000") + " | " + ((int)expMax).ToString("000");
}
//Next button
public void AdvanceText()
{
currentLineNumber++;
LevelUp();
UpdateAll();
}
//Back button
public void BackwardsText()
{
if (currentLineNumber >= 1)
{
currentLineNumber--;
}
Debug.Log (currentLineNumber);
UpdateAll();
}
//Input field
public void SetQuote()
{
}
public void UpdateAll()
{
TextLabelGO.text = dialogStrings[currentLineNumber];
QuoteNumber.text = "Quote " + ((int)currentLineNumber).ToString() + " of " + ((int)quoteMaxNumber).ToString();
}
public void LevelUp()
{
if (expMax <= 100 && expCurrent >= expMax)
{
level++;
expCurrent = 0;
expMax = expMax + 5;
}
LevelCurrent.text = ((int)level).ToString();
LevelExp.text = ((int)expCurrent).ToString("000") + " | " + ((int)expMax).ToString("000");
}
}
This is the properties for my Next button, my Back button is similar except it shows ButtonClicky.BackwardsText instead.
Not sure what I'm doing wrong. :(
Answer by Renoki · Nov 28, 2014 at 08:26 PM
Okay uh, figured out what my issue was. Apparently I had to set currentLineNumber as a static variable. I apologize for the noob question!