The question is answered, right answer was accepted
Instantiating at specific float values only?
Is it possible to instantiate a prefab at a random transform position chosen from 3 different vector 3's? Right now I have List prefabList = new List(); public GameObject Prefab0; public GameObject Prefab1; public GameObject Prefab2; public GameObject Prefab3;
void Update () { CreatePrefab (); }
void CreatePrefab()
{
InstantiationTimer -= Time.deltaTime;
if (InstantiationTimer <= 0 && playpanel.activeInHierarchy)
{
int prefabIndex = UnityEngine.Random.Range (0, 4);
Instantiate(prefabList [prefabIndex], new Vector3 (0,0,0) , Quaternion.identity);
InstantiationTimer = 2f;
}
}
This causes a random prefab from the list to spawn at (0,0,0). However, I want to be able to have them randomly spawn at (-5,0,0), (0,0,0,) or (5,0,0). I know that Random.Range can spawn between the positions (-5,0,0) and (5,0,0), but I only want those specific values. For example, I don't want something spawning at (-3.5,0,0) or (4,0,0). Is it possible to choose three spawn points that are randomly chosen?
Answer by jcantoine · Apr 04, 2018 at 08:39 PM
Make a list with your Vector3 positions. And then call
int r = Random.Range(0, list.Count);
Vector3 position = list[r]
Follow this Question
Related Questions
I get an error message every time i run this script? Any ideas? 0 Answers
My code is instantiating many prefabs, i only want one. 1 Answer
Generating prefabs at origin that are children of moving GameObjects? 2 Answers
How to spawn a prefab at a duplicate objects location 1 Answer
Create individual prefabs 1 Answer