Array out of index, but shouldn't be
I have a grid that is 15 wide and 20 high, I also have a method in which checks and updates this grid when something comes into it and that object becomes apart of the grid, but the moment the object is instantiated the grid array becomes out of index... I've looked over it and it shouldn't, but as usual I'm most likely not seeing something. Here's the code:
using System;
using UnityEngine;
using System.Collections;
namespace GameManager
{
public class GameManager:MonoBehaviour
{
Instantiator instance;
public static int gridWidth = 15;
public static int gridHeight = 21;
private static Transform[,] gridMatrix;
void Start()
{
instance = FindObjectOfType<Instantiator>();
gridMatrix = new Transform[gridWidth, gridHeight];
instance.Spawn();
}
public void UpdateGrid(Tetro tetro)
{
for (int y = 0; y < gridHeight; y++)
{
for (int x = 0; x < gridWidth; x++)
{
if (gridMatrix[x, y] != null)
{
if (gridMatrix[x, y].parent == tetro.transform)
{
gridMatrix[x, y] = null;
}
}
}
}
foreach (Transform mino in tetro.transform)
{
Vector2 pos = RoundOffPositions(mino.position);
if (pos.y < gridHeight)
{
gridMatrix[(int)pos.x, (int)pos.y] = mino;
}
}
}
public Transform getChildPositionInGrid(Vector2 pos)
{
if (pos.y > gridHeight - 1)
{
return null;
}
else
{
return gridMatrix[(int)pos.x, (int)pos.y];
}
}
public bool CheckIsInGrid(Vector2 pos)
{
return (pos.x >= -7 && pos.x <= 7 && pos.y >= -10);
}
public Vector2 RoundOffPositions(Vector2 pos)
{
return new Vector2(Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y));
}
void Update()
{
}
}
}
and:
using System;
using UnityEngine;
using System.Collections;
public class Tetro:MonoBehaviour
{
Instantiator instance;
GameManager.GameManager management;
public bool hasLimitedRotation;
void Start ()
{
instance = FindObjectOfType<Instantiator>();
management = FindObjectOfType<GameManager.GameManager>();
StartCoroutine(Fall());
}
IEnumerator Fall()
{
foreach(Transform mino in transform)
{
while (mino.position.y > -10)
{
yield return new WaitForSeconds(1);
transform.position -= new Vector3(0, 1);
if (!checkEachChildPosition())
{
transform.position += new Vector3(0, 1);
instance.Spawn();
enabled = false;
yield break;
}
}
}
}
public void CheckInput()
{
if (transform.position.y > -9)
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0);
if (!checkEachChildPosition())
{
transform.position += new Vector3(-1, 0);
}
else
{
management.UpdateGrid(this);
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0);
if (!checkEachChildPosition())
{
transform.position += new Vector3(1, 0);
}
else
{
management.UpdateGrid(this);
}
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.Rotate(0, 0, 180);
if (hasLimitedRotation)
{
transform.Rotate(0, 0, -90);
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
transform.position -= new Vector3(0, 1);
if (!checkEachChildPosition())
{
transform.position += new Vector3(0, 1);
}
else
{
management.UpdateGrid(this);
}
}
}
}
bool checkEachChildPosition()
{
foreach (Transform mino in transform)
{
Vector2 pos = management.RoundOffPositions(mino.position);
if (management.CheckIsInGrid(pos) == false)
{
return false;
}
if (management.getChildPositionInGrid(pos) != null && management.getChildPositionInGrid(pos).parent != transform)
{
return false;
}
}
return true;
}
void Update ()
{
CheckInput();
}
}
this is the error on the stack:
IndexOutOfRangeException: Array index is out of range.
GameManager.GameManager.getChildPositionInGrid (Vector2 pos) (at Assets/Scripts/GameManagers/GameManager.cs:52)
Tetro.checkEachChildPosition () (at Assets/Scripts/Tetros/Tetro.cs:94)
Tetro+<Fall>c__Iterator0.MoveNext () (at Assets/Scripts/Tetros/Tetro.cs:25)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Note: The gridHeight is 21 because I was checking the values to make sure that it wasn't a problem with the values. The same problem occurred at the various values.
You don't have copy-able code here and don't seem to be making any sort of print statements / etc to help debug your situation.
You're giving indices based on a rounded float, which is weird. The simple check would be to swap those two and see if you're just backwards on your x,y.
Okay, I tried switching them, got the same error. I ran some debugging logs to trace the problem. But to no avail. I still got the same problem. I took out the Round() method because I saw what you meant, it was unnecessary, but that didn't change a thing either. I'm assu$$anonymous$$g that I may not have the Game$$anonymous$$anager on the right object for it to read the grid properly, so I'm gonna try that and see what happens.
Answer by SoraMahiro · Dec 19, 2016 at 11:51 PM
Found the first problem, at line 57 of GameManager, the correct return is supposed to be return (pos.x >= 0 && pos.x < gridWidth && pos.y >= 0)
, however now I have the issue that after the first object falls in the grid and lands, the grid updates but the next objects that spawn all get stuck at the top and they continue to spawn. The pieces do not fall. I've never had this much trouble making a game before.
Answer by dpoly · Dec 20, 2016 at 05:41 AM
There are several possibilities here. Do not thrash around trying random fixes -- diagnosis before treatment.
First, find out the exact values that are 'out of range', using Debug.Trace() or the debugger;
Second, make sure that grid width and height are what you expect -- public variables can be changed.
Be particularly careful about mixing ints and floats: best to case to int before checking in range.
Check both height and width, not just height.