prismspecs
prismspecs

Reputation: 1489

Spawn random game object?

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

Upvotes: 4

Views: 12549

Answers (3)

Marina Markous
Marina Markous

Reputation: 11

Use a of a random number generator. Assign each obstacle a "case" and within each case tell it what to do. In my script I needed a variety of platforms to appear randomly, but at a set interval.

using UnityEngine;

public class Generate : MonoBehaviour { public GameObject prefab1;

public GameObject prefab2;

public GameObject prefab3;

public GameObject prefab4;

public GameObject prefab5;

public GameObject prefab6;

public int platform;

// Use this for initialization
void Start()

{
    InvokeRepeating("CreateObstacle", 1f, 1.5f);    //generate
}

void CreateObstacle()
{
    platform = Random.Range (1, 7);             //radom number generator b/w 1 and 7
    float randomY = Random.Range(-5f, 5f);      // appear b/w -5 and 5 in y-axis
    float rightScreenBound = 10;                // spawn this much right of the screen

    switch (platform)
    {
    case 1: 
        Instantiate(prefab1, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity); 
        break;
    case 2:
        Instantiate(prefab2, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    case 3: 
        Instantiate(prefab3, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    case 4: 
        Instantiate(prefab4, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    case 5: 
        Instantiate(prefab5, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    case 6: 
        Instantiate(prefab6, new Vector3(rightScreenBound, randomY, 0), Quaternion.identity);
        break;
    }


}

Upvotes: 1

user819640
user819640

Reputation: 250

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.

Upvotes: 3

Steven Mills
Steven Mills

Reputation: 2381

Another method of loading GameObjects during runtime is by placing items in a folder named "Resources" and then using the call bellow:

GameObject obstacle = Resources.Load("myGameObject") as GameObject;

if the item is within a folder inside the Resources folder, then you simply call:

GameObject obstacle = Resources.Load(@"myFolder/myGameObject") as GameObject;

Be aware however that using this method, there will be a slight delay in the item's spawning while it's loaded into the game.

Upvotes: 1

Related Questions