so i've constructed an exercise for myself to create an array that scrolls between it's values whenever the user presses space. it took my about 4 hours of trial and error but i've finally managed to make it work. now i'm trying to make the scrolling method not execute if the arrays index goes out of bounds, but i can't seem make it work.
i've tried many things, one of which was to put a condition inside the input if statement that adds that it will only work if the index is (which is an int called currentText) is < text.Length. this was the if statement's condition:
if (Input.GetKeyDown (KeyCode.Space) && currentText < text.Length){
ScrollText(); //which is a method i've created.
}
else if ( Input.GetKeyDown (KeyCode.Space) && currentText ≥ text.Length){
gameObject.SetActive(false);
this was the original code. i've changed it to be as follows, but either way the else if / else statements don't get executed and i recieve an out of bounds error. this is the current code, thanks:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour
{
public GameObject[] text = new GameObject[3];
int currentText = 0;
void Start()
{
text[currentText].SetActive(true);
}
void ScrollText()
{
if (currentText < text.Length)
{
text[currentText].SetActive(false);
currentText++;
text[currentText].SetActive(true);
}
else
{
gameObject.SetActive(false);
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
ScrollText();
}
}
}