- Home /
How to Instantiate objects automatically???
Hello friends, I have Total 10 objects. Which have to be generated automatically and randomly. There has to be specific time interval in between object generation. If any of those object touch the character, they should be destroyed, or else if they pass through a particular position they should be destroyed. I just don't know what to do. Please if any one can help me out, It will be a great help to me. Thanks in advance.
Please, specify better... your objects are Prefabs? Do you want to do this in code or only in the editor?
For instancing through code, you can use GameObject.Instantiate.
For doing it after a period of time, call Invoke with a time parameter
For destroying things when touching the player, use Colliders
First of all thanks for the reply. Actually i want to make an array of all those objects and then instantiate them randomly. I have got the logic to destroy the objects. But, not for the instantiate. It will be a great help if you can help me with the code. Thanks
Answer by Dmitry Pyalov · Mar 25, 2014 at 11:26 AM
Create a prefab of your game object.
Store the reference to prefab in your generation script or put the prefab in Resources folder and load it via Resources.Load()
Instantiate game objects from prefab via Object.Instantiate
???
PROFIT
First of all thanks for the reply. Actually i want to make an array of all those objects and then instantiate them randomly. I have got the logic to destroy the objects. But, not for the instantiate. It will be a great help if you can help me with the code. Thanks
Create an array of GameObject in your master script. Fill it with prefabs you want. Then randomly choose one of them and instantiate it via Object.Instantiate.
Or put the prefabs inside Resources folder :) $$anonymous$$ake a list of resource paths and then Resource.Load them and instantiate via Object.Instantiate.
Thanks for taking interest brother. I have already made an array of prefabs. But, i don't know how to generate objects randomly from that array.
If you can help me with the script it will be a great help for me.
Something like:
var prototype = this.prefabs[Random.Range(0, prefabs.Length)];
var gameObject = (GameObject)Object.Instantiate(prototype, new_location, new_rotation);
Sorry, don't have Unity opened right now. New_location and new_rotation should be random generated by you somehow, e.g. using Random class.
Your answer
Follow this Question
Related Questions
Instantiate GameObject on local axis rather than world axis 1 Answer
2D Platform Generation: Spawning Platforms Consecutively 3 Answers
There is a generation lag 1 Answer
Generate cubes in 5 areas 1 Answer
Checking if object intersects? 1 Answer