A
A
Alexander Andropov2018-06-07 20:11:05
C++ / C#
Alexander Andropov, 2018-06-07 20:11:05

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++;
        }
    }

It generates a word, breaks it into symbols, randomly arranges them. Here the Word variable is defined, as a result, the Hint and Path variable comes from the button, the OnClick event.
The first time, when starting the scene, the method works well, without errors.
After the word is guessed, I reset all variables to zero using another method.
Используется 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];
    }

As a result, I click on the button with already other Word, Hint, Path variables and an error occurs:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index

I can't figure out what the problem is and why this is happening. The first lines work just fine, as the Path changes.
Please tell me what I'm doing wrong, I broke my whole head. Thanks a lot in advance !

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2018-06-07
@LongOf

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);

Given that AllSymbols.Count == 0, Random.Range(0, 0) I don't even know what it will return. In any case, you do not have AllSymbols[0], much less AllSymbols[-1]

D
d-stream, 2018-06-07
@d-stream

Breakpoint on the line with the problem and see the value of index.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question