- Home /
Randomly spawning objects,Random Object and Spawn Point?
I want all 6 objects to spawn in. I want it to pick 1 of 8 spawns and assign all 6 objects to those 8 different spawns without spawning 2 in the same spot. heres my current code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RandomSpawn : MonoBehaviour { public Transform[] spawns; public GameObject[] objects;
private int randomPref;
private int randomSpawn;
void Start()
{
randomPref = Random.Range(0, 6);
randomSpawn = Random.Range(0, 8);
Instantiate(objects[randomPref], spawns[randomSpawn].transform.position, Quaternion.identity);
}
}
And it works. But not what i need it to do,I have 6 objects, I need them to to pick 1 of 8 spawn points.
So here's the code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RandomSpawn : MonoBehaviour { public Transform[] spawns; public GameObject[] objects;
private int randomPref;
private int randomSpawn;
void Start()
{
randomPref = Random.Range(0, 6);
randomSpawn = Random.Range(0, 8);
Instantiate(objects[randomPref], spawns[randomSpawn].transform.position, Quaternion.identity);
}
}
And it works, It picks 1 of 6 objects and spawns it in 1 of 8 spawns, but i want it to pick all 6 and assign all 6 to 1 on of 8 random spawn points without spawning 2 objects in the same spot.
Answer by Hellium · May 23, 2019 at 06:08 AM
using UnityEngine;
using System.Collections.Generic;
public class RandomSpawn : MonoBehaviour
{
public Transform[] spawns;
public GameObject[] objects;
void Start()
{
SpawnObjects( objects, spawns );
}
private void SpawnObjects( GameObject[] gameObjects, Transform[] locations, bool allowOverlap = true )
{
List<GameObject> remainingGameObjects = new List<GameObject>( gameObjects );
List<GameObject> freeLocations = new List<GameObject>( locations );
if( locations.Length < gameObjects.Length )
Debug.LogWarning( allowOverlap ? "There are more gameObjects than locations. Some objects will overlap." : "There are not enough locations for all the gameObjects. Some won't spawn.");
while( remainingGameObjects.Count > 0 )
{
if( freeLocations.Count == 0 )
{
if( allowOverlap ) freeLocations.AddRange( locations );
else break ;
}
int gameObjectIndex = Random.Range( 0, remainingGameObjects.Count );
int locationIndex = Random.Range( 0, freeLocations.Count );
Instantiate(gameObjects[gameObjectIndex], locations[locationIndex].position, Quaternion.identity);
remainingGameObjects.RemoveAt( gameObjectIndex );
freeLocations.RemoveAt( locationIndex );
}
}
}
Answer by TawhidOOF · Mar 05 at 11:54 AM
Here is the updated code without errors.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class random : MonoBehaviour
{
public Transform[] spawns;
public GameObject[] objects;
void Update()
{
spawn(objects, spawns);
}
void spawn(GameObject[] gameObjects, Transform[] locations, bool allowOverlap = true){
List<GameObject> remainingGameObjects = new List<GameObject>( gameObjects );
List<Transform> freeLocations = new List<Transform>( locations );
if( locations.Length < gameObjects.Length )
Debug.LogWarning( allowOverlap ? "There are more gameObjects than locations. Some objects will overlap." : "There are not enough locations for all the gameObjects. Some won't spawn.");
while( remainingGameObjects.Count > 0 )
{
if( freeLocations.Count == 0 )
{
if( allowOverlap ) freeLocations.AddRange( locations );
else break ;
}
int gameObjectIndex = Random.Range( 0, remainingGameObjects.Count );
int locationIndex = Random.Range( 0, freeLocations.Count );
Instantiate(gameObjects[gameObjectIndex], locations[locationIndex].position, Quaternion.identity);
remainingGameObjects.RemoveAt( gameObjectIndex );
freeLocations.RemoveAt( locationIndex );
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612201901im_/https://answers.unity.com/themes/thub/images/avi.jpg)