I have a TGRidObject error for out of bounds

Any clue to why I am getting this error. Thanks

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GridSystem<TGridObject>
{
    private int width;
    private int height;
    private float cellSize;

    private TGridObject[,] gridObjectArray;

    public GridSystem(int width, int height, float cellSize, Func<GridSystem<TGridObject>, GridPosition, TGridObject> createGridObject)
    {
        this.width = width;
        this.height = height;
        this.cellSize = cellSize;
        gridObjectArray = new TGridObject[width, height];
        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                GridPosition gridPosition = new GridPosition(x, z);
                gridObjectArray[x, z] = createGridObject(this, gridPosition);
            }
        }
    }

    public Vector3 GetWorldPosition(GridPosition gridPosition)
    {
        return new Vector3(gridPosition.x, 0, gridPosition.z) * cellSize;
    }

    public GridPosition GetGridPosition(Vector3 worldPosition)
    {
        return new GridPosition(
            Mathf.RoundToInt(worldPosition.x / cellSize),
            Mathf.RoundToInt(worldPosition.z / cellSize)
        );
    }

    public void CreateDebugObjects(Transform debugPrefab)
    {
        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                GridPosition gridPosition = new GridPosition(x, z);
                Transform debugTransform = GameObject.Instantiate(debugPrefab, GetWorldPosition(gridPosition), Quaternion.identity);
                GridDebugObject gridDebugObject = debugTransform.GetComponent<GridDebugObject>();
                gridDebugObject.SetGridObject(GetGridObject(gridPosition));
            }
        }
    }

    public TGridObject GetGridObject(GridPosition gridPosition)
    {
        return gridObjectArray[gridPosition.x, gridPosition.z];
    }

    public bool IsValidGridPosition(GridPosition gridPosition)
    {
        return  gridPosition.x >= 0 &&
                gridPosition.z >= 0 &&
                gridPosition.x < width &&
                gridPosition.z < height;
    }

    public int GetWidth()
    {
        return width;
    }

    public int GetHeight()
    {
        return height;
    }
}

Look in the stack trace, what caused that error? Did you forget to add checks to see if the position is valid?

here is the stack trace. Ive checked the scripts and im unsure what happened

Levelgrid ln 101 must be calling getgridobject with an x or z that is too large or small.

Would that mean, I need to change the function to something that specifies the gridPosition

(LevelGrid 101) GridObject gridObject = gridSystem.GetGridObject(gridPosition);

    gridObject.SetInteractable(interactable);

}

Track back to a function that is specifying a x or z. To find the issue.

You can just trap the error in getgridobject to just return null if the x or y is outside the bounds.

But there is likely a bug here you want to find.

I’m going to go through the lesson again. I cant figure this one out .

If you share more code, we can help.
If you click the error, you will see a stack trace that shows which function got you to where the error happened.

thanks. I went back through the lesson. No problems this time. ?? so not sure what happened

InteractSphere.cs (Start function) line 22.

Edit Never mind. Appears to be fixed

As a rule of thumb, before calling GetGridObject(GridPosition), always check first and call IsValidGridPosition(GridPosition) to ensure that the position is valid.

Privacy & Terms