How to spawn randomly with multiple gameobject
Hi, i know how to spawn 1 game object randomly between times but the problem is if i have more than 1 or more (im not decide yet how many)game object to spawn, how do i write for the code in c#? Please teach.
Answer by OsmiousH · Aug 20, 2016 at 01:36 AM
Always remember that spawn is used in network terms and Instance is used in local terms
Ok I know Javascript but ill try my best to Define my script in C#
import UnityEngine;
import System.Collections;
public GameObject[] ObjectsToSpawn;
void Update(){
if(Input.GetKey("space")){
Object.Instantiate(Objects[Random.Range(0,ObjectsToSpawn.length)]);
}
}
Now
Go to inspector
Find the script over there
Drag and drop the objects in the drop down called ObjectsToSpawn
Now How does this work?
In the Update function which is called every frame (Whenever the screen is repainted)
there is an if statement that waits for the Keyboard Input Space as a condition
When the condition is met it randomly chooses and Instantiates (Creates) a new GameObject from the list of GameObjects
Oh and plz mark my answer as correct
Answer by OsmiousH · Aug 20, 2016 at 01:42 AM
Go to inspector
Find the script over there
Drag and drop the objects in the drop down called ObjectsToSpawn
Your answer
Follow this Question
Related Questions
Script Spawning Double? 1 Answer
Spawn gameObject horde, modify concentration of spawned objects. 1 Answer
How can I make an object clone in the time that I indicate it to appear? 0 Answers
How to spawn power ups at random places on random time internvals 1 Answer
Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers