Dragging multiple blocks to a grid.

Hey guys, I’m making a block puzzle game where a randomly generated set of coloured blocks needs to be dragged into a grid (think Columns). I need to drag all three items together so I was planning to make all of them children of an empty GameObject, but I can’t seem to make this work at all.

public class SpawnBlock : MonoBehaviour
{

    public GameObject[] blocks;
    

    void Start()
    {
        
        
        Vector3[] spawnPositions = new[] { new Vector3(-5, 0, 0), new Vector3(-4, 0, 0), new Vector3(-3, 0, 0)};
        
        for (int i = 0; i < 3; i++)
        {
            Instantiate(blocks[Random.Range(0,6)], spawnPositions*, Quaternion.identity);*

}

}
}
I’ve tried making a GameObject to parent the blocks to but I’m getting errors whatever I try. Any help would be most welcome.

You can set the parent of an object by using the SetParent method
So in your code that would be:

 public class SpawnBlock : MonoBehaviour
{

public GameObject[] blocks;

//This is the reference to the object that will be the parent of the instantiated blocks
public Transform parent;

void Start()
{


Vector3[] spawnPositions = new[] { new Vector3(-5, 0, 0), new Vector3(-4, 0, 0), new Vector3(-3, 0, 0)};

for (int i = 0; i < 3; i++)
         {
             GameObject newObject = Instantiate(blocks[Random.Range(0,6)], spawnPositions*, Quaternion.identity);*

//Set the parent of ‘newObject’
newObject.transform.SetParent(parent);
}

}
}

That’s fantastic, thank you very much.