Scripting: Spawning

Step 1
In this lesson we'll work on creating a robot and making it shoot two types of objects: Objects and Particles.
Step 2
Load/Open your previous project, you should have working teleporters, switches, and keys that enable keys.
Step 3
Optional Challenge: Create your own robot or use my robot.
Step 4
Attach your robot to your player.
Step 5
Let's create our shooting object, it's up to you to decide what it'll be. I'm just going to use a sphere.

Size it about the same size as the robot.

Step 6
Give the Sphere a rigid body with NO gravity and NO kinematic.
Step 7
Now we want to spawn copies of this 3D sphere on a click or button press. Create a new C# Script, named "Shoot"
Step 8
Open up the Shoot Script and copy this code into it, we'll explain what it does on the next step.
Step 9
Let's see what does code does... We'll come back to edit more of this later!
Step 10
Go back to Unity, select your robot body and create an empty object.
Step 11
Name the empty object as "SpawnLocation"
Step 12
Select the Robot body again and add the Shoot C# Script to it.
Step 13
Let's fill these empty slots with some objects and values!
Temp1546210311 Temp1546210326
  • Spawn object should be your PREFAB 3D Sphere that you made earlier.
  • Spawn Location should be the empty object you created on your robot.
  • Speed is 1000
  • Attack Rate Per Second is 5.
Step 14
Lastly we want to move the spawn location outside of the robot body. Make it go in front of the robot.
  • The red line should extend outwards.
Check Your Work
Step 15
Press play and HOLD down the LEFT mouse button! Your sphere spawning should look similar to this.
Step 16
Oh Uh, we might have a glitch! When your spheres or cubes hit the other objects, you teleport!
Step 17
To fix this, let's open up the TeleportTo.cs script!
Step 18
First let's change this Collision "door" variable to "other"
Temp1546211293 Temp1546211310
Step 19
Now we create an if statement IF that other's tag is a "Player"
Check Your Work
Step 20
Save this TeleportTo.cs script and hit play to test out your game again!

Your red enemy cubes don't teleport you anymore of another object touches it, only if it touches YOU then it will teleport you!

Step 21
Do the same fix to your SwitchBetween.cs, loadLevel.cs and EnableNext.cs script!
Step 22
Now well try spawning a Particle instead of a gameObject! So let's temporarily remove the Shoot script from your robot!
Step 23
Select your robot and create a Particle System.
Step 24
Resize your Particle System and make sure it can collide with objects.
Step 25
Now for Collision, make sure Send Collision Messages is checked, then change renderer type to Mesh & Sphere Shape.
Temp1546213840 Temp1546213869

You might to change your start size to 0.3 so we can see the collisions better later on.
* And also change the world simulation to "World" instead of "Local" if you haven't.

Step 26
Also we don't want our particle to hit our player! In Collision of the particle, uncheck "Player" in "Collides With"
Check Your Work
Step 27
Hit play and it should looks like this.
Step 28
Next we are going to create a script and add force so that our particle collisions can actually move the cubes!

Create a new C# Script called "particleShoot"

Step 29
Open the particleShoot script and type this code in.
Step 30
This is what the first two parts of this code does. The next part is going to expained more in detail and pictures.
Step 31
(You may need to click to read this image) Here is what the other part of the code does.
Step 32
Save the script, go back to Unity and attach this script onto your particle system!.
Step 33
For Ps, Click and drag the particle system itself into your slot! For Power put "3".
Check Your Work
Step 34
Click play and see if this works!

If it doesn't seem to work or if your particles are too slow, increase your particle speed and maybe increase "power" from your script.

Step 35
Just like how we spawned objects, we did it using the left mouse click, can you do able/disable for this particle?
Step 36
Can you make the robot's rotating cylinders move faster when it's shooting and move slower when it's idle?

1. Do this for spawning objects, so your Shoot.cs script.
2. Create another Rotating cylinder and animate it to spin faster, make this inactive.
3. In your shoot script you are going to make two public gameObject variables, one for slow rotating cylinder and one for fast cylinder.
4. Enable the slow one (and disable the fast one) only when you're not holding left click.
5. Disable the slow one (and enable the fast one) only when you're holding left click.

Temp1546255877 Temp1546255978
Step 37
You are finished with this lesson! Click submit and move onto the next lesson on UI Variables!
Scripting: Spawning Info


MVCode Clubs

Created By

Nepgear tina (instructor)


Unity Advanced

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach