Problem Setting Instantiated GameObject's Parent

I’m working on doing procedural level generation using unity tilemaps and I’m having an issue getting the instantiated rooms to unparent from the spawner and parent to the grid. I have tried several different approaches with zero luck. Ideally the spawned rooms will be locked in the position they are spawned and parented to the grid but currently no matter what I do they stack on top of each other and move with the spawner.

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

public class LevelGeneration : MonoBehaviour
{
    //changing for tilemap
    private GameObject grid;

    public Transform[] startingPositions;
    public GameObject[] rooms;

    private int direction;
    public float moveAmountX;
    public float moveAmountY;

    private float timeBtwnRoom;
    public float startTimeBtwRoom = 0.25f;

    private void Start()
    {
        //tilemap integration
        grid = GameObject.FindGameObjectWithTag("Grid");

        int randStartingPos = Random.Range(0, startingPositions.Length);
        transform.position = startingPositions[randStartingPos].position;


        GameObject newRoom = Instantiate(rooms[0], transform.position, Quaternion.identity);
        newRoom.transform.parent = null;
        newRoom.transform.SetParent(grid.transform);
        direction = Random.Range(1, 6);
       
    }

    private void Update()
    {
        if (timeBtwnRoom <= 0)
        {
            Move();
            timeBtwnRoom = startTimeBtwRoom;
        }
        else
        {
            timeBtwnRoom -= Time.deltaTime;
        }
    }

    private void Move()
    {
        if (direction ==1 || direction == 2)
        {
            //Move Right
            Vector2 newPos = new Vector2(transform.position.x + moveAmountX, transform.position.y);
            transform.position = newPos;
        } else if (direction == 3 || direction == 4)
        {
            //Move Left
            Vector2 newPos = new Vector2(transform.position.x - moveAmountX, transform.position.y);
            transform.position = newPos;
        } else if (direction == 5)
        {
            //Move Down
            Vector2 newPos = new Vector2(transform.position.x, transform.position.y - moveAmountY);
            transform.position = newPos;
        }

        GameObject newRoom = Instantiate(rooms[0]) as GameObject;
        newRoom.transform.position = transform.position;
        newRoom.transform.SetParent(grid.transform);

        direction = Random.Range(1, 6);
    }
}

There are two different approaches used in my code and I have tried many others as well and no matter what I’ve tried rooms parent to spawner. The first room spawns on lines 29-31 and the rest spawn from the code on lines 68-71. Really pulling my hair out on this one. If anyone has any insight on this it would be greatly appreciated.

dont use FindGameObjectWithTag( to assign your grid, instead try making it a public variable and assign it in the inspector

2nd - set parent first, and only change the transform after setting the parent

3rd - theres an extra parameter on the instantiate method that you can add so that i instantiates already parented

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);

Ok this totally worked! And yea I know gameobject.find isn’t the best to use but for some reason my head was in prefab mode even though the spawner is not a prefab. So I did end up dragging it in the inspector and it fixed it. But I also noticed when I did this that I made the grid tag and didn’t actually assign it to the grid so It likely would’ve worked if I had just done that. I’ve been using unity for well over a year regularly and still manage to make this mistake from time to time. Can’t believe I didn’t think to check that when I was trying to fix this bug. Anyways thanks for your help and also showing me that extra parameter I didn’t know about in Instantiate!

So you know for the future, for general scripting questions please use the Scripting forum. We try to keep the 2D forum for specific 2D related issues.

I’ll move your post.

Thanks.