- Home /
Spawn from array
Hello, I am trying to make a simple unity game just to learn something :) I want to spawn some obstacles from an array at a given position. How can i do this? I looked around on google and here but i have not found a simple solution for me. Help is appreciated :) I use above all c#. Thank you!!!
What exactly do you store in your array?, the gameobjects?
Answer by Landern · Mar 02, 2015 at 06:36 PM
This isn't a script writing service.
There are a LOT of examples of this all over the forums and UA... people blog about it all the time. The examples may not be 100% what you want, but you will find components of what you want to do i'm sure.
If you clicked on the spawn tag that you put on this post, you will find tons of examples from random, to percentage based, etc.
The gist of it is: Have a script with an array or list of type GameObject or Transform. Make it a public field. Set the field length in the inspector. Drag prefabs from your project to the various index locations.
Get a reference to this component in the script that randomizes the spawning gameobject and location/position.
Use Random.range to pick an index from your List (Random.range(0, yourList.Count(or length if using array)).
Instantiate using object from index.
Rinse repeat.
Answer by curiouspers · Mar 02, 2015 at 07:30 PM
GameObject NewInstance = Instantiate (objArray[Random.Range(0,objArray.Length-1)],
new Vector3 (x, y, z),
Quaternion.identity) as GameObject;
NewInstance.name += "_"+x+"_"+y+"_"+z;
Your answer
Follow this Question
Related Questions
Multiple random spawns from an array 1 Answer
Spawning Objects Using An Array. 1 Answer
Instantiating prefab at child (spawnlocations are arrays) 2 Answers
add spawned GameObjects in an array C# 1 Answer
onTrigger spawn object C# 3 Answers