Answer the question
In order to leave comments, you need to log in
Why is the Argument is out of range error?
Good afternoon friends. I use this method in my game:
public void GenerateLevel()
{
Word = "GALAXY"; Hint = "Hint for answer"; Path = "Sprites/Img1";
ImageWord = Resources.Load<Sprite>(Path);
ImageDefault.sprite = ImageWord;
tempCell = new GameObject[Word.Length];
checkAddDel = new int[Word.Length];
RandomSym = new string[14 - Word.Length];
answerSym = new string[Word.Length];
foreach (char ch in Word.ToUpper().ToCharArray())
{
int rnd = Random.Range(0, AllSymbols.Count);
while (AllSymbols[rnd].GetComponent<Text>().text != "-")
rnd = Random.Range(0, AllSymbols.Count);
AllSymbols[rnd].GetComponent<Text>().text = ch.ToString();
totalSymbol++; // Считаем символы
answerSym[answChar] = AllSymbols[rnd].GetComponent<Text>().text;
answChar++;
}
foreach (GameObject go in AllSymbols) // Заполняем пустые кубики
{
if (go.GetComponent<Text>().text == "-")
{
go.GetComponent<Text>().text = ((char)Random.Range(65, 91)).ToString(); // 65,91 - коды Латинских символов
RandomSym[randChar] = go.GetComponent<Text>().text;
randChar++;
}
}
foreach (char ch in Word.ToUpper().ToCharArray())
{
tempCell[indexTemp] = Instantiate(PrefabCharCell);
tempCell[indexTemp].transform.SetParent(GridCellPos, false);
int tName = indexTemp;
tempCell[indexTemp].GetComponent<Button>().onClick.AddListener(() => DeleteSymbol(tName));
tempCell[indexTemp].gameObject.name = indexTemp.ToString();
indexTemp++;
}
}
Используется csharp
public void CleanLevel()
{
for (int i = 0; i < Word.Length; i++)
tempCell[i].GetComponentInChildren<Text>().text = "";
Word = ""; Hint = ""; Path = ""; totalSymbol = 0; indexTemp = 0; numChar = 0; tempAnswer = 0; flChar = 0; randChar = 0; answChar = 0;
AllSymbols.Clear();
GoodAnswerPanel.SetActive(false);
checkAddDel = new int[Word.Length];
RandomSym = new string[14 - Word.Length];
answerSym = new string[Word.Length];
}
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
Answer the question
In order to leave comments, you need to log in
At the first start, your AllSymbols list seems to be filled with something.
Before the second run you clear it: AllSymbols.Clear()
And then
int rnd = Random.Range(0, AllSymbols.Count);
while (AllSymbols[rnd].GetComponent<Text>().text != "-")
rnd = Random.Range(0, AllSymbols.Count);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question