Issue with GetGridPosition

I continue to get this error and I cannot figure out what is causing this. I’ve went through the course multiple times and even just rebuilt from prior lesson. can someone help


What does your GridSystem class look like? Maybe you misspelled the name and didn’t name it exactly “GetGridObject”
Or maybe you just forgot to save the file after writing that function.

This is my GridSystem

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

public class GridSystem
{
    private int width;
    private int height;
    private float cellSize;

    private GridObject[,] gridObjectArray;
    public GridSystem(int width, int height, float cellSize)
{
    this.width = width;
    this.height = height;
    this.cellSize = cellSize;
    gridObjectArray = new GridObject[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] = new GridObject( this, gridPosition);
        }
    }
}

    public Vector3 GetWorldPosition(int x, int z)
    {
        return new Vector3(x, 0, 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++)
             {
                  GameObject.Instantiate(debugPrefab, GetWorldPosition(x, z), Quaternion.identity);
             }
        }
    }
}

Is getgridposition INSIDE the class? The code you pasted implies it is not.

I think I solve it. I was missing my GridDebugObject

In the code you posted there is no GetGridObject(...) method

This needs a GetGridObject(GridPosition) method. Try adding this to the class:

public GridObject GetGridObject(GridPosition gridPosition)
{
    return GridObject[gridPosition.x, gridPosition.y];
}

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms