Instance of object isn't a game object?

I’m trying to set the parts of a chunk as children of the chunk prefab.

World.cs creates chunks from my Map prefab, and each Map prefab creates a grid from my Square prefab. But the squares can’t be made children of the Map. The error is:

gridPart.transform.parent = (GameObject) this;
Cannot convert type ‘Map’ to ‘UnityEngine.GameObject’

Here is the relevant code that causes the error.

(I can’t get the code to format correctly…sorry about that)


    public virtual IEnumerator DrawMap(){ //creates the grid of terrain tiles according to WIDTH and HEIGHT
    
    for (float i = transform.position.x; i < transform.position.x+width; i++) {
    
        for (float j = transform.position.y; j < transform.position.y+height; j++){
    
          Vector3 pos = new Vector3 (i, j, 0);
    
          bool squareAtLocation = Square.SquareIsAtPosition(pos);
    
          if(squareAtLocation){
    
            yield return 0;
    
          }
    
          else{
    
            GameObject gridPart = (GameObject) Instantiate(squarePrefab, new Vector3 (i, j, 0), Quaternion.identity);
    
            gridPart.transform.parent= (GameObject) this;
    
          }
    
        }
    
      }
    
      yield return 0;
    
    }

“this” refers to the script that’s attached to the GameObject. Also the parent has to be a Transform component, not a GameObject. You should use:

gridPart.transform.parent = transform;

‘this’ will be a reference to the script, not to the game object. In addition, ‘parent’ needs to be assigned a transform, not a game object. I think you want this:

gridPart.transform.parent = transform;

Additional note, use:

yield return null;

Not:

yield return 0;