Scripting: Collisions

Step 1
In this lesson we'll learn about colliding and collecting Coins!
Step 2
Load/Open your previous project from the [No coding] Unity Intermediate Course.
Step 3
Create a Cylinder with a Yellow colored Material and shape it into a coin.
Step 4
Rename this Cylinder as "Coin"
Step 5
Give it a Rotating Animation
Step 6
Save this Coin as a prefab.
Step 7
Click and drag the Prefab coin into your scene, Let's create about 10 of them.

Hit Play and you should have about 10 coins all rotating.

Step 8
Let's change the prefab coin's Tag to Collectible, by Selecting the "Prefab", Not the individual coins!!
Temp1545596950 Temp1545596980 Temp1545597022 Temp1545597066

After we make a new tag, our object won't be changed to that tag just yet, so click on the coin prefab again and change it to the new tag called "Collectible"

Step 9
Next we want to create a script on the player to collect the coins. So select the player and make a new script.

Name it as "CollectCoins"

Step 10
Now that we have a script, click this gear icon.
Step 11
And Edit Script.
Check Your Work
Step 12
Either Visual Studio, Sublime, or Monodevelop should open up. Your code should look like this.
Step 13
The start function is where we set our variables. The update function is just like our draw() function in JavaScript!
Step 14
Add another function called OnCollisionEnter as shown.
Step 15
Inside the OnCollisionEnter() function, create this if statement.
Step 16
Let's see how this code works...

if(other.gameObject.tag == "Collectible"){


  • "other" is ANY object that our player touches.
  • "other.gameObject" Looks into that object's inspector.
  • "other.gameObject.tag" Looks into that object's inspector's TAG.

  • if(other.gameObject.tag == "Collectible") This checks if that object's TAG is called "Collectible"

Step 17
Lastly lets add this to our OnCollisionEnter function. This means whenever we touch a "Collectible" it disappears.
Check Your Work
Step 18
Save your script and run your project. Run into the coins and they should disappear.
Step 19
After a coin disappears, we should spawn a particle shining effect where the coin disappeared!
Step 20
First we want a particle prefab since we're gonna use it again and again. Create a Sparkle particle

You can google "Sparkle" on google images and make sure to search for a transparent color.
Or you can use the sparkle I have here.

Download Sparkle.png
Step 21
Save your Sparkle particle as a prefab by clicking and dragging it into your project panel.
Step 22
Go back to our CollectCoins.cs script, and add this to the top, before the start() function. Save the script.
Step 23
Go back to Unity, select to player to see the script, you now see an empty slot. Click and drag the sparkle prefab in it
Temp1545602079 Temp1545602152

Click and drag your spark prefab into this slot.

Step 24
Go back to the CollectCoins.cs script and add this new code.
Step 25
Let's see what all this code means...
  • Create a copy of the Sparkle prefab.
  • put it at other.transform.position (The coin's location)
  • rotation stays the same "other.transform.localRotation"
  • Put all of this into a "temp" gameObject.
  • Destory "temp" after 1f (1 second)
Check Your Work
Step 26
Save the script, go back to your unity project and press Run, try collecting the coins now!
Step 27
You are finished with this lesson! Click submit and go to the next lesson on creating Teleports!
Scripting: Collisions 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