Hello there,
I started using Unity last week. I have done nothing but sleep four hours or so a day and work on a project. I have it all done and I’m happy with it, however I’m running into a critical issue that crashes my game. My game is essentially 190 or so images that you can click left and right on and change the shader and text tooltip to read 0/7, 1/7, 2/7 etc or however many you need of each item. It’s a checklist essentially.
The problem arises when you click the “next” button when you’re already 7/7 and you go beyond the array, outside of the index, however you need to put it. I get an “IndexOutOfRangeException: Index was outside the bounds of the array” error and it crashes. Since it saves automatically outside of the range, I’m screwed without resetting the button manually.
How do I “cap” the function of the next and previous buttons so that they cannot go outside of the index in either direction? Keep in mind out of the 190 buttons they all have different values. You need 20/20 of some things.
Here’s my code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Cycle : MonoBehaviour
{
public GameObject[] background;
public int index;
void Update()
{
if(index >= 20)
index = 20 ;
if(index < 0)
index = 0 ;
if(index == 0)
{
background[0].gameObject.SetActive(true);
}
if(index == 1)
{
background[1].gameObject.SetActive(true);
}
if(index == 2)
{
background[2].gameObject.SetActive(true);
}
if(index == 3)
{
background[3].gameObject.SetActive(true);
}
if(index == 4)
{
background[4].gameObject.SetActive(true);
}
if(index == 5)
{
background[5].gameObject.SetActive(true);
}
if(index == 6)
{
background[6].gameObject.SetActive(true);
}
if(index == 7)
{
background[7].gameObject.SetActive(true);
}
if(index == 8)
{
background[8].gameObject.SetActive(true);
}
if(index == 9)
{
background[9].gameObject.SetActive(true);
}
if(index == 10)
{
background[10].gameObject.SetActive(true);
}
if(index == 11)
{
background[11].gameObject.SetActive(true);
}
if(index == 12)
{
background[12].gameObject.SetActive(true);
}
if(index == 13)
{
background[13].gameObject.SetActive(true);
}
if(index == 14)
{
background[14].gameObject.SetActive(true);
}
if(index == 15)
{
background[15].gameObject.SetActive(true);
}
if(index == 16)
{
background[16].gameObject.SetActive(true);
}
if(index == 17)
{
background[17].gameObject.SetActive(true);
}
if(index == 18)
{
background[18].gameObject.SetActive(true);
}
if(index == 19)
{
background[19].gameObject.SetActive(true);
}
}
public void Next()
{
index += 1;
for(int i = 0 ; i < background.Length; i++)
{
background[i].gameObject.SetActive(false);
background[index].gameObject.SetActive(true);
}
Debug.Log(index);
}
public void Previous()
{
index -= 1;
for(int i = 0 ; i < background.Length; i++)
{
background[i].gameObject.SetActive(false);
background[index].gameObject.SetActive(true);
}
Debug.Log(index);
}
}
And the manager for this code…
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CycleManager : MonoBehaviour
{
public List<Cycle> CycleList;
void Start()
{
if (!PlayerPrefs.HasKey("CycleValues"))
return; // do nothing if there's no save value
string[] loadedStrings = PlayerPrefs.GetString("CycleValues").Split(' ', StringSplitOptions.RemoveEmptyEntries);
for(int i=0; i<CycleList.Count; i++)
CycleList[i].index = int.Parse(loadedStrings[i]);
}
void OnApplicationQuit()
{
string saveString = "";
foreach(Cycle c in CycleList)
saveString += c.index + " ";
PlayerPrefs.SetString("CycleValues", saveString);
PlayerPrefs.Save();
}
}
Go easy on me, I barely understand the basics but am passionate about this project.
Thank you so much for the help.