Array index is out of range?
I am doing a language changing system with buttons where if you click the language you want it changes to that language. I am doing this by using different arrays for each language and when I click one of them it assigns all of the main array string's values to that language's array values.
It works, however I am getting an array out of bounds for some reason. Can anyone point me in the right direction?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Mainscript : MonoBehaviour {
public Text Test;
public string[] names;
public string[] Ptnames;
public string[] LanguageHolder;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
UpdateStrings();
}
public void changePt()
{
for(int i = 0; i <= Ptnames.Length;i++)
{
LanguageHolder[i] = Ptnames[i];
}
}
public void changeEnglish()
{
for (int i = 0; i <= names.Length; i++)
{
LanguageHolder[i] = names[i];
}
}
public void UpdateStrings()
{
Test.text = LanguageHolder[0];
}
}
Both, when I click either buttons the respective array gets out of range.
Answer by PrisVas · Jun 28, 2016 at 08:13 PM
Try this:
public string[] names = new string[];
public string[] Ptnames = new string[];
public string[] LanguageHolder = new string[];
Gives me an error of "Array creation must have size or array initializer"
Oooohh I see now! Should be: "i < Ptnames.Length" and "i < names.Length"
Oh ok that's it. $$anonymous$$any thanks! How would I do this with a resizable list ins$$anonymous$$d, if you don't $$anonymous$$d answering?
Your answer
Follow this Question
Related Questions
[C#] Array out of bounds 1 Answer
What is the problem with the loop? 0 Answers
Storing a Gameobject from array into a Gameobject variable giving NullReferenceException 3 Answers
Fields not populated during OnValidate on Editor startup 2 Answers
Cannot convert UnityEngine.GameObject to UnityEngine.GameObject 1 Answer