I have 5 spawn points and three game objects, my goal is to spawn a random object at each of the locations. so far i have a script that will randomly select one of the three objects and spawn it at one of the 5 locations at random. I could use a bit of help to convert the script to select a different object at each spawn point.
here is what i have so far
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
//create an array of spawn points, assigned in inspector
public Transform[] collectableSpawnPoints = new Transform[5];
//create an array of collectables to choose from
public GameObject[] items = new GameObject[3];
// Use this for initialization
void Start () {
SpawnCollectables();
}
// Update is called once per frame
void Update () {
}
//selects spawn point
public Transform GetCollectableSpawnPoint()
{
//randomly selects a point out of the array
int index = Random.Range(0, collectableSpawnPoints.Length);
//returns the selected point
return collectableSpawnPoints[index];
}
//selects object to spawn
public GameObject GetCollectable()
{
//selects one of the items from the array
int index = Random.Range(0, items.Length);
//returns the object selected
return items[index];
}
// spawns the random object on the random point
public GameObject SpawnCollectables()
{
//selects the spawn point
Transform spawnPoint = GetCollectableSpawnPoint();
//selects the object
GameObject collectable = GetCollectable();
//creates the object selected on the point selected
GameObject c = Instantiate(collectable, spawnPoint.position, spawnPoint.rotation) as GameObject;
//spawns the object
return c;
}
Following your example, because you are spawning at random locations anyway, there is no need to also spawn a random item. So you can just iterate the items using an indexer to make sure each spawned item is unique. I have edited your code as an example.
It will start at a random index, and then proceed from that index to select items.
Your script however will only spawn one random item. If you want to spawn all the items, I included a method to do so. private void SpawnAllCollectables()
public class GameManager : MonoBehaviour {
//create an array of spawn points, assigned in inspector
public Transform[] collectableSpawnPoints = new Transform[5];
//create an array of collectables to choose from
public GameObject[] items = new GameObject[3];
//keeps track of the items spawn index
private int itemIndex = 0;
// Use this for initialization
void Start () {
itemIndex = Random.Range(0, items.Length);
SpawnCollectables();
}
// Update is called once per frame
void Update () {
}
//spawns all the collectables from the items array
private void SpawnAllCollectables()
{
//iterates through the number of collectables in items array
for (int i = 0; i < items.Length; i++)
{
//spawns a unique collectable
SpawnCollectables();
}
}
//selects spawn point
public Transform GetCollectableSpawnPoint()
{
//make sure the item index is within the item array range
itemIndex = itemIndex % items.Length;
//returns the selected point
return collectableSpawnPoints[itemIndex++];
}
//selects object to spawn
public GameObject GetCollectable()
{
//selects one of the items from the array
int index = Random.Range(0, items.Length);
//returns the object selected
return items[index];
}
// spawns the random object on the random point
public GameObject SpawnCollectables()
{
//selects the spawn point
Transform spawnPoint = GetCollectableSpawnPoint();
//selects the object
GameObject collectable = GetCollectable();
//creates the object selected on the point selected
GameObject c = Instantiate(collectable, spawnPoint.position, spawnPoint.rotation) as GameObject;
//spawns the object
return c;
}
hi;
first of all u don’t really need to create another function for every lane of your code that may just make u more confuse about what u are going to di , you can do it simply like this in 1 lane;
public GameObject SpawnCollectables()
{
GameObject c = Instantiate(items[Random.Range(0, items.Length)], collectableSpawnPoints[Random.Range(0, collectableSpawnPoints.Length)].position, spawnPoint.rotation) as GameObject;
//spawns the object
return c;
}
for your question do u want to spawn all the balls randomly in your Transform ?
if that’s the idea then u don’t need to randomly select the balls u can just write a loop and go through all of your balls and Instantiate them;