Grid x,z showing 0,0 on in the game window

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.transform.GetComponent<GridDebugObject>();

                Debug.Log("x: " + x + "; z:" + z);
                Debug.Log(GetGridObject(gridPosition));

                gridDebugObject.SetGridObject(GetGridObject(gridPosition));
            }
        }
    }

This Debug.Log(GetGridObject(gridPosition)); returns x=0, z=0

Always? For every object? That means there’s something wrong with your GetGridObject function, how are you getting the grid object? Should be accessing the array in the x,z position
If that is correct then perhaps you made a mistake when creating the grid and created the object 0,0 on every position

GetGridObject function is same as in tutorial,

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

Can you please elaborate the second part?

It will. For the first item. I ran this and it works fine. What does GetGridObject() do? What does the ToString() of the gridObject look like?

Then it looks like you probably accidentally set every single gridObjectArray position with a reference to the gridObject on 0,0
Look at the code initializing the grid, cycling through each grid position, how are you assigning the GridObject to the array?
Or maybe you just didn’t assign the X,Z on each Grid Object so it’s defaulting to 0,0

1 Like

I matched my code with the project files and both were same. I deleted the prefab of GridDebugObject and recreated it, now its working fine.

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

Privacy & Terms