Spawn a random object on more than one position
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;
}
Answer by Skyking · Apr 17, 2017 at 08:50 AM
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;
}
I think you misunderstood. I'm trying to eli$$anonymous$$ate the random spwan point and simply spawn 1 random item on all 5 spawn points ins$$anonymous$$d of 1 random item at 1 of the 5 points. However your answer might have unintentionally given me what I need.
Gotcha. In that case you would just need to iterate the spawn points and select one random collectable to spawn. So something like:
// will spawn one random collectable on each of your spawn points
private void SpawnCollectables()
{
// first pick what random collectable you want to spawn
GameObject randomCollectable = items[Random.Range(0, items.Length)];
// next iterate all the spawn points, and spawn that item on each of them
foreach (Transform spawnPoint in collectableSpawnPoints)
{
Instantiate(randomCollectable, spawnPoint.position, spawnPoint.rotation);
}
}
That code may be simple but is a thing of beauty. Thanks! I just wished I saw it before I figured something else out. I ended up changing things into a for loop and used my "i" iteration in collectableSpawnPoints[i] to run the randomization and spawn for each spawn point. Giving a random item at each spawn.
Answer by Cuttlas-U · Apr 17, 2017 at 06:29 AM
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;
First off, please use proper English when answering to avoid further confussion. Second, I have the code arranged like this for a reason, the only changes im interested in are ones that make the change in looking for, but thanks for the tip. As for my question, as I stated I want to spawn a random item at all of my speak points ins$$anonymous$$d of one of the randomly selected spawn points.
The reason for the higher number of functions is for debugging ease in the first steps of figuring out how to work it, as well as reusability, as I may choose to implement those functions elsewhere in later assignments.