Getting error GridObject to GridDebugObject

Hi all. I am fairly new to coding and very new to Unity. I have been following along with the turn based game course and now I’m stuck on

Assets\Scripts\Grid\GridSystem.cs(49,47): error CS1503: Argument 1: cannot convert from ‘GridObject’ to ‘GridDebugObject’

I’m fairly certain it’s something simple i’m overlooking. Has anyone else had this issue or can y’all help me fix it?

Paste in your GridSystem.cs script and we’ll take a look. Be sure to highlight line 49 with a comment.

So, I ended up copy pasting y’alls code from the repos and that fixed the error I was having. I think I had an issue in GridDebugObject.cs. Now i’m having an issue with y’alls code where instead of getting coordniates i’m getting “GridObject”

Here’s my GridDebugObject.cs

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

public class GridDebugObject : MonoBehaviour
{

    [SerializeField] private TextMeshPro textMeshPro;


    private GridObject gridObject;

    public void SetGridObject(GridObject gridObject)
    {
        this.gridObject = gridObject;
    }

    private void Update()
    {
        textMeshPro.text = gridObject.ToString();
    }

}

and here is my GridSystem.cs

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

public class GridSystem
{
    int width;
    int height;
    float cellSize;
    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];
    }
}

Check GridObject.cs. There’s an override on ToString() that you should implement

Fantastic. Thank you!

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

Privacy & Terms