Basically, I want to instantiate a special object “objectToSpawn” at a random position in an array. Then I want to instantiate a random item “randomItem” at every other position.
I think I’ve figured out how to instantiate just one “objectToSpawn” but I’m not sure how to spawn items at every other point except for the one where the objectToSpawn was instantiated.
Does that make sense?
Here’s what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomPositionSpawner : MonoBehaviour
{
private GameObject spawnPoint;
public GameObject[] spawnPoints;
public GameObject objectToSpawn;
private int spawnPointSelector;
public static bool spawnAllowed;
public bool shouldSpawnItems;
public GameObject[] itemsToSpawn;
private int randomItem;
// Use this for initialization
void Start()
{
spawnPoint = this.gameObject;
spawnPointSelector = Random.Range(0, spawnPoints.Length);
spawnPoint.transform.position = spawnPoints[spawnPointSelector].transform.position;
spawnAllowed = true;
SpawnObject();
}
void SpawnObject()
{
if (spawnAllowed)
{
Instantiate(objectToSpawn, spawnPoint.transform.position, Quaternion.identity);
if (shouldSpawnItems)
{
foreach (GameObject point in spawnPoints)
{
randomItem = Random.Range(0, itemsToSpawn.Length);
Instantiate(itemsToSpawn[randomItem], point.transform.position, Quaternion.identity);
}
}
spawnAllowed = false;
}
}
}
Ok so I’ve made some progress here. The only thing I’m not sure about is how to skip the first location. I added a shuffle method and just need to figure out how to not spawn at the first location but spawn at the rest of them.
Here’s what I’ve got so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomPositionSpawner : MonoBehaviour
{
//public RoomCenter roomCenter;
private GameObject spawnPoint;
public GameObject[] spawnPoints;
public GameObject objectToSpawn;
private int spawnPointSelector;
public static bool spawnAllowed;
private GameObject tempGO;
public bool shouldSpawnItems;
public GameObject[] itemsToSpawn;
private int randomItem;
// Use this for initialization
void Start()
{
spawnPoint = this.gameObject;
spawnPointSelector = Random.Range(0, spawnPoints.Length);
spawnPoint.transform.position = spawnPoints[spawnPointSelector].transform.position;
Shuffle();
spawnAllowed = true;
SpawnObject();
}
public void Shuffle()
{
for (int i = 0; i < spawnPoints.Length; i++)
{
int rnd = Random.Range(0, spawnPoints.Length);
tempGO = spawnPoints[rnd];
spawnPoints[rnd] = spawnPoints[i];
spawnPoints[i] = tempGO;
}
}
void SpawnObject()
{
if (spawnAllowed)
{
Instantiate(objectToSpawn, spawnPoint.transform.position, Quaternion.identity);
if (shouldSpawnItems)
{
foreach (GameObject point in spawnPoints)
{
if(//////////)
{
randomItem = Random.Range(0, itemsToSpawn.Length);
Instantiate(itemsToSpawn[randomItem], point.transform.position, Quaternion.identity);
}
}
}
spawnAllowed = false;
}
}
}
OK! So I think I’ve got this working as intended, except for one thing. Looks like the items are spawning in random positions. Any idea why this could be? Could it have anything to do with “spawnPoint = this.gameObject;”?
Here’s my updated script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomPositionSpawner : MonoBehaviour
{
private GameObject spawnPoint;
public GameObject[] spawnPoints;
public GameObject objectToSpawn;
private int spawnPointSelector;
public static bool spawnAllowed;
private GameObject tempGO;
public bool shouldSpawnItems;
public GameObject[] itemsToSpawn;
private int randomItem;
// Use this for initialization
void Start()
{
spawnPoint = this.gameObject;
Shuffle();
spawnPoint.transform.position = spawnPoints[0].transform.position;
spawnAllowed = true;
SpawnObject();
}
public void Shuffle()
{
for (int i = 0; i < spawnPoints.Length; i++)
{
int rnd = Random.Range(0, spawnPoints.Length);
tempGO = spawnPoints[rnd];
spawnPoints[rnd] = spawnPoints[i];
spawnPoints[i] = tempGO;
}
}
void SpawnObject()
{
if (spawnAllowed)
{
Instantiate(objectToSpawn, spawnPoint.transform.position, Quaternion.identity);
if (shouldSpawnItems)
{
for (int i = 1; i < spawnPoints.Length; i++)
{
randomItem = Random.Range(0, itemsToSpawn.Length);
Instantiate(itemsToSpawn[randomItem], spawnPoints[i].transform.position, Quaternion.identity);
}
}
spawnAllowed = false;
}
}
}