Spawn random game object?
c#, unity-game-engine
Solution
What you have looks fine so far. Having a public array attached to your monobehaviour will let you drag prefabs from the inspector which you can use to spawn
In your method 'SpawnWall()' you would just need to select a prefab from your array
GameObject randPrefab = spawnObject[spawnObjectIndex];
Then you would use
GameObject newObstacle = GameObject.Instantiate(randPrefab) as GameObject;
And do whatever position code you want through its transform
I would recommend renaming your array to something like 'obstaclePrefabs' as 'spawnObject' doesn't really describe a list of obstacles to spawn.
Problem
My problem is that I want my obstacle spawner, which is at a set distance in front of the player's spaceship, to randomly select from a set of different obstacle prefabs each time it instantiates an obstacle. I've found plenty of threads on how to randomize position, but that's not what I'm looking for. I've seen a lot of references to lists and tags but I can't seem to figure out how to implement them correctly. I'll post my spawner script below with comments where I "think" changes are supposed to be made. ``` using UnityEngine; using System.Collections; public class RandomSpawner : MonoBehaviour { public GameObject[] spawnObject; //somehow change this to incorporate multiple gameobject prefabs, will an array support that? //Would I create public variables for each prefab I want to be randomly chosen from, or would those be contained in the array above? public float xRange = 1.0f; public float yRange = 1.0f; public float minSpawnTime = 1.0f; public float maxSpawnTime = 10.0f; void Start() { Invoke("SpawnWall", Random.Range(minSpawnTime,maxSpawnTime)); } void SpawnWall() { float xOffset = Random.Range(-xRange, xRange); float yOffset = Random.Range(-yRange, yRange); int spawnObjectIndex = Random.Range(0,spawnObject.Length); //above line will have to change to reflect whatever goes above Start, possibly below as well ```