If I wanted to have a timer that, every 3 seconds, spawned either a blue sphere or a red sphere, at random X/Y/Z location inside certain boudaries, how would I do this? I’m not new to programming, I’m just new to Unity3D’s API - and help is really appreciated =D. I’ve been searching how to do this for quite a while now.
combination of coroutines, instantiate, and random.range (look 'em up in the docs).
Use InvokeRepeating for every 3 seconds, a GameObject[ ] array for the red and blue spheres, Random.Range for selecting the spheres and the boundaries (or possibly Random.insideUnitSphere depending on what sort of boundary you mean), and Instantiate for spawning.
–Eric
I’m looking up those things in the Unity Manual as we speak. I was hoping someone could give a small sample of code (or pseudo code) to help me out, because I’m feeling pretty clueless right now. =(
None of this is code, just some breadcrumbs to follow. if you need further help on anything, just ask!
For timing: I would use a co-routine in a “Spawner” script with a while loop that returns a new WaitForSeconds(3). Launch it from Awake() or Start(). This will just run forever once launched (assuming you don’t add something which quits). All it has to do is instanciate a prefab reference and set the new instance’s position. To set a random location, just get a random number for x and z (keep y at 0), then set the position to this coordinate. e.g.
instance.transform.position = new Vector3(randX, 0, randY);
In fact, there is an example in the Unity script reference docs here:
Just put the spawner script on a game object and start the scene.
You could always use a random number as a sort of dice role where if the number is > 0.5, you make one choice, else another.
You can either have two prefabs, one for each sphere, or use the same sphere and alter the material. I would recommend two spheres though, with UVs and have them share the same material (atlas your red and blue on to one UV set, even if a solid color) because it will allow unity to batch the instances when rendering. If you change the color by accessing ‘renderer.material’ you will stop that object from batching (cause another drawcall). if you use ‘sharedMaterial’ then you will change everything using the material (like editing the source).
InvokeRepeating is simpler/more efficient.
–Eric
I hate to derail a thread with detail at this level (either above will work), but I have a hard time believing InvokeRepeating is more efficient. I would guess it just launches a co-routine with a delegate. Wouldn’t that just be more calls internally for the same outcome. Anyone care to do a speed test?
Here are the docs for InvokeRepeating by the way:
I don’t care if I am right or wrong on this point. I’m just curious (though not curious enough to do the test unless I need to!)
I already did a speed test a while ago, and InvokeRepeating is more CPU efficient than coroutines (can’t remember by how much though). That’s why I wrote what I did.
–Eric
Touche.
Thanks!