Unity - Working with GameObjects
Project fountain:
We will create a little script that will spawn new gameObjects in a fountain. Once we've made the script we can use it to spawn any kind of object. The first version is going to be rather hard on the computer's graphics system :)
Then we'll see how Unity has its own built in system for creating particle effects like this - a powerful system that doesn't require any additional scripting. |
Step one - create the two GameObjects
- Create two objects - a Sphere and a Cube. Reset their positions so that they overlap each other in the centre of the world. Make the cube quite small - but it's up to you.
- Rename the Sphere to 'SphereSpawner'. To rename an object, find it in the Hirearchy window and carefully click on it until it's highlighted. Then you can type a new name. You don't have to do this, but after a while it's useful to be able to see what things do by having sensible names.
- Add a new C# script to the SphereSpawner. Call the script something like 'Spawner' and double click it to open in Mono
Here's the code that we start with.
The line public Transform whatToSpawn; creates a sort of slot, where we can drag any object that we like. In the Update function, there's an if statement that's totally useless at the moment, it just is always true and always runs the code in the {} braces. That code creates a copy of the object in our slot and aligns it with the parent object. |
To get a good effect, add some physics to the cube. In the Unity editor, click on the cube to select it, then from the main menu choose 'Component', 'Physics' and 'Rigidbody'. This means that the object will obey physics and gravity.
Try it out
Controlling the fountain with a key
Remember that useless 'if' statement above? We're going to make it useful. In this case we'll only create new clones when the user holds down the 'f' key. It's easy, just replace the (true) with this:
Try it out
There's a problem. These cubes never die
They look cool when you can see them, but then they fall off the world and head downwards forever. In time, all these forgotten cubes are going to cause lag, serious lag. We need them to self-destruct.
We need to be careful here. Destroying the object is easy, we just use the script command 'Destroy' and give it a time to destroy after being created. However this will also destroy our original object, the one that everything else is a copy of. To get round this problem we need to create what is called a 'Prefab' and clone that instead.
We need to be careful here. Destroying the object is easy, we just use the script command 'Destroy' and give it a time to destroy after being created. However this will also destroy our original object, the one that everything else is a copy of. To get round this problem we need to create what is called a 'Prefab' and clone that instead.
If you run this program, it won't work. That's because the cube that is the source of all the others, the one we dragged into the slot in the spawner, has killed itself and isn't there any more.
To fix this, drag the cube from the Hirearchy window down into the project pane.
To fix this, drag the cube from the Hirearchy window down into the project pane.