I am trying to make a game where the exit will randomly spawn. I have the idea that there will be 5 different locations that the exit can spawn in but I have no idea how to do this. The exits prefab name is “Exit”.
The easiest way to create it is to create an array of Vector3 for the position and then Instantiate with random value
something like(C#):
public Vector3[] spawnPoints; // Array of Vector3 for position, set in the inspector
public GameObject exitPrefab; // Exit prefab
void Start()
{
int spwanPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(exitPrefab, spawnPoints[spwanPointIndex], exitPrefab.transform.rotation);
}
the more “generic” way is to create a game object that will ground empty game objects that are located where the spawn points are and randomize that
should look like(again C#):
public GameObject spwanPointsObject; // The Parent Game Object for the spawn points objects
public GameObject exitPrefab; // Exit prefab
void Start()
{
Transform[] spawnPoints = spwanPointsObject.GetComponentsInChildren<Transform>();
int spwanPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(exitPrefab, spawnPoints[spwanPointIndex], exitPrefab.transform.rotation);
}
hope that helped
Here is a little script that I made (C#):
using UnityEngine;
using System.Collections;
public class EnemySpawner : MonoBehaviour {
public GameObject Exit;
void Start()
{
Vector3 position = new Vector2(Use Random Range and switch function to give the location);
Instantiate(Exit, position, Quaternion.identity);
} }