make an object appear at one of the chosen places before the game starts
Hello i am a new Unity user and im trying to make a basic random level, for that i have an object what i want to make it spawn at the differents location i have choose but make it spawn when the game start (whitout timer directly when game start) and not one time, in a random range of 10 at 30 times( for example, i want to make spawn 10 at 30 object among the locations i have choose, i want make spawn only one prefab, it's enough hard like that xD, i hope explain that clearly ^^) if someone know how to make a system like that can you Help me please :D
if i don't have explain what i want clearly, ask me which part is not understandable. :)
Answer by Fewpwew130 · Jul 22, 2019 at 02:22 PM
Hey!
If I understood you correctly, you want:
to Spawn 10-30 objects at random position, when the game starts (i.e. when you press "Play" in the inspector).
If this is the case, I would suggest the following steps:
make a prefab of the object you want to "Clone"
create a public GameObject variable in the script
when the game is in stop mode (not playing) drag and drop (with the mouse) your prefab into the script, into that 2) variable slot
edit the script: in Void Start() { } add the code, something like: for (int i = 0; i < 30; i++) { // The_name_of_your_gameobject_variable.transform.position = new Vector3(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)); //}
Maybe the script is incorrect, I did not try it.
P.S. Here is some related question: https://answers.unity.com/questions/577132/random-position-for-gameobject.html
@Fewpwew130 Hey thanks for your anwser, i would like to create 10 at 30 object at random position when i press Start Button but that take a random position among wich i have selected. if the upload of my scene work ^^ you can see the spawner position (it's the white cube) among these spawner, 10 at 30 object will appear, so i have to take theses emplacement and drag and drop to my script, choose wich prefab i want to make appear among these spawn, but the problem after that is that I don't know how to get them to make it work in the script. I don't know if it's possible to make it work that way. ^^'
So, as I understood: you have 30 positions and 1 object. This 1 object should be given a random position (out of those 30), when you press Start.
In this case you should: assign each spawn and then randomly select one of them. Assigning could be done automatically, if you instantiate via script, something like: for (int i = 0; i < 10; i++) { //creates 10 columns for ((nt j = 0; i < 3 j++){ //creates 3 rows GameObject spawn = new GameObject; List list_of_spawns = new List(); list_of_spawns.Add(spawn); spawn.tranform.position = new Vector3(0 +i , 0 + j, 0); } }
then in the void Start() for example: int random_variable = Random.Range(0,list_of_spawns.count); The_name_of_your_gameobject_variable.transform.position = list_of_spawns(random_variable).transform.position;
or it could be done by hand: public GameObject Spawn1; public GameObject Spawn2; public GameObject Spawn3; public GameObject Spawn4; public GameObject Spawn5; public GameObject Spawn...; public GameObject Spawn30;
public List list_of_spawns = new List();
list_of_spawns.Add(Spawn1);
list_of_spawns.Add(Spawn2);
list_of_spawns.Add(Spawn3);
list_of_spawns.Add(Spawn4);
list_of_spawns.Add(Spawn5);
list_of_spawns.Add(Spawn...);
list_of_spawns.Add(Spawn30);
and then the same: int random_variable = Random.Range(0,list_of_spawns.count); The_name_of_your_gameobject_variable.transform.position = list_of_spawns(random_variable).transform.position;
Again, the code was from the top of my head. $$anonymous$$aybe it is incorrect. If you have questions, please ask.
that's work ! thank's a lot :D, I could never have created a script like this without help I had the idea but the knowledge necessary to build the script is missing ^^'.
thanks for your time and your good explication :)