Answer the question
In order to leave comments, you need to log in
Why IndexOutOfRangeException: Array index is out of range?
Good day. I get the error IndexOutOfRangeException: Array index is out of range, but I don’t understand why.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Gameplay : MonoBehaviour {
string Word;
public GameObject PrefabCharCell;
public GameObject[] tempCell;
public Transform GridCellPos;
void Start ()
{
Word = "Meet";
foreach (char ch in Word.ToUpper().ToCharArray())
{
for (int i = 0; i < Word.Length; i++)
{
tempCell[i] = Instantiate(PrefabCharCell);
tempCell[i].transform.SetParent(GridCellPos, false);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
It is necessary to allocate memory for the tempCell array
tempCell = new GameObject[Word.Length];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question