- Home /
Object generator?
I'm currently developing a simple 3D game for iPhone and I'm need of an "object generator". Basically I have an object with a script in it and i want a new copy of it to appear at a random location every second.
How would I go about doing this?
Thanks in advance!
Answer by Tetrad · Jun 17, 2010 at 09:20 PM
You simply need to write a script that every second instantiates your object prefab somewhere in the scene.
Here's the function: http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html
Here's the reference on prefabs: http://unity3d.com/support/documentation/Manual/Prefabs.html
For "spawn a model every second", you can do something simple like this (C#):
public GameObject objectPrefab;
void Start() { StartCoroutine( SpawnObjects() ); }
IEnumerator SpawnObjects() { while( true ) { Instantiate( objectPrefab, /however you need to determine a valid random position/, Quaternion.identity ); yield return new WaitForSeconds( 1.0f ); } }
}
Thanks! So I would create a prefab "objectPrefab" in which I would put my duplicate object, where would I put this script? (Sorry I'm a bit new to all of this Unity stuff. I'm more used to program$$anonymous$$g in Objective-C)
Just create an object in the scene, put that code in a class, and attach that script to the object. That empty object will be your object generator. You'll have to attach the prefab to the input (public variable) of your script in the inspector window.
Your answer
Follow this Question
Related Questions
iPhone game with free Unity? 1 Answer
A node in a childnode? 1 Answer
How to make an ingame weapon/ammo shop 0 Answers
Unity iPhone Tutorial? 1 Answer
Need some help with JavaScript 2 Answers