I’m making a game where the player goes through a maze, trying to find a key somewhere within it that unlocks a door at the end of the maze. However, I want the key to show up randomly at one of about 20 different locations upon the start of the game so that an experienced player wont know where the key is. How can I do this?
Create multiple empty gameObjects and position them throughout the level wherever you want the keys to have a chance of spawning. Then create a main empty GameObject call it manager or something, create a new c# script and put this code into it:
public List<GameObject> possibleSpawnPoints = new List<GameObject>();
public GameObject keyPrefab;
// Use this for initialization
void Start () {
int chosenSpawnPoint = Random.Range(0, possibleSpawnPoints.Count);
GameObject instantiatedKey = GameObject.Instantiate(keyPrefab, possibleSpawnPoints[chosenSpawnPoint].transform.position, Quaternion.identity, transform);
}
Open the manager gameobject in the inspector and drag all of the empty spawnpoint objects you made into the possiblespawnpoints section and set the keyprefab to your key prefab