make a game object list and picking from that list

I’m looking to spawn objects randomly inside a room. I’m quite a novice and i’ve searched for bit and heres what I have. The way i want it to work is that I have gameobjects set and a picker collects one from the list of game objects and places the gameobject it got into the room.

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

public class PcPartsGenerator : MonoBehaviour
{
GameObject spawnPoints;
GameObject currentPoint;
int index;

// Start is called before the first frame update
void Start()
{
spawnPoints = GameObject.FindGameObjectsWithTag(“points”);
index = Random.Range(0, spawnPoints.Length);
currentPoint = spawnPoints[index];
print(currentPoint.name);

}

// Update is called once per frame
void Update()
{

}
}

look up Instantiate. this means to spawn an object.

I’m thinking you’re wanting something like the following:

public List<GameObject> objectsToSpawn;
List<GameObject> itemLocations;

void Start()
{
    for(int i = itemLocations.Count; i > 0; i--)
        {
            Instantiate( objectsToSpawn[Random.Range(0, objectsToSpawn.Count) ], itemLocations[i].transform.position, Quaternion.identity );
            // optionally you may add the following:
            // Destroy(itemLocations[i]);
        }
}

This will take each location and spawn one of the predefined objects at that location.

1 Like