Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created May 9, 2024 10:47
Show Gist options
  • Save TheCuttlefish/76bb8b4182a1b3aecaf575a085ee20e2 to your computer and use it in GitHub Desktop.
Save TheCuttlefish/76bb8b4182a1b3aecaf575a085ee20e2 to your computer and use it in GitHub Desktop.
spawn things (once!)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnObject : MonoBehaviour
{
public List<Transform> possiblePositions = new List<Transform>();
public List<GameObject> allThings = new List<GameObject>();
void Start()
{
// do something as long as objects in the array exist
for (int i = possiblePositions.Count; i > 0; i--)
{
int rndObject = Random.Range(0, allThings.Count);// generate a number from 0 to possible objects in the list (that changes)
Instantiate(allThings[rndObject], possiblePositions[0].position, Quaternion.identity);// spawn object at the 1st spawn point
possiblePositions.RemoveAt(0); // remove spawn position that has been used!!
allThings.RemoveAt(rndObject); // remove the spawned object that has been used!!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment