I am having trouble with the grid

I finished the Grid Object lecture; however, I ran into a problem making only one GridDebugObject appear. I tried fixing it. When that didn’t work, I resorted to copying and pasting the lecture changes; however, that did not help. Any help would be much appreciated! (:slightly_smiling_face:

It’s only creating a single debug object. Can you show the CreateDebugObjects(...) you created? It doesn’t seem to be looping through all the objects

Here is my Testing script. I do believe this is what is supposed to create the GridDebugObject if I am not mistaken.

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

public class Testing : MonoBehaviour
{

    [SerializeField] private Transform gridDebugObjectPrefab;

    private GridSystem gridSystem;

    private void Start()
    {
        gridSystem = new GridSystem(10, 10, 2f);
        gridSystem.CreateDebugObjects(gridDebugObjectPrefab);
        Debug.Log(new GridPosition(5, 7));
    }

    private void Update()
    {
        Debug.Log(gridSystem.GetGridPosition(MouseWorld.GetPosition()));
    }
}

Let’s take a look at your GridSystem.cs as well. That’s where the actual GridDebugObjects are created (and the actual Grid Size is set).

Sorry for my bad formatting last time.

Here 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(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 GridObject GetGridObject(GridPosition gridPosition)
    {
        return gridObjectArray[gridPosition.x, gridPosition.z];
    }
}

In your picture, you’re showing an X: -1, Z: -3, which I am assuming is the location of the MouseWorld from the code involved.
Can you show the whole console after you run. I’m interested in seeing if there are any errors in the Console once it’s run.

There it is. That error is stopping any more objects from being created. It looks like your prefab does not have a GridDebugObject script

Thank you so much! It is working now. (:slightly_smiling_face:)

Curiously enough, it looks like the prefab is set in the first image.

Let’s set up a couple of Debugs to see what might be happening:

First, in Testing.Start()

    private void Start()
    {
        if(gridDebugObjectPrefab)
        {
             Debug.Log($"Testing, GridDebugObjectPrefab = {gridDebugObjectPrefab.gameObject.name}");
        } else
        {
             Debug.Log($"Testing - No GridDebugObject.");
        }
        gridSystem = new GridSystem(10, 10, 2f);
        gridSystem.CreateDebugObjects(gridDebugObjectPrefab);
        Debug.Log(new GridPosition(5, 7));
    }

Then in GridSystem.CreateDebugObjects before the for loops

if(debugPrefab)
{
    Debug.Log($"GridSystem CreateDebugObjects, prefab = {debug.Prefab.gameObject.name)");
} else
{
    Debug.Log("GridSystem CreateDebugObjects, no prefab!!);
}

I think it’s the default values of the text. Hugo set the text to 1,2 for a default so all grid visuals said 1,2 until they started adding the positions after

image
That’s why I’m not sure why it’s working. I suspect the one in the scene is a holdover (prefabbed, but not deleted).

1 Like

Makes sense, but Hugo references the transform. The object’s there, but it didn’t have the GridDebugObject script.

Privacy & Terms