Code Breakdown

Sheridan Hanby
Aug 29, 2017 · 2 min read

This code is used on the pickup objects within the roll-a-ball tutorial. It makes it so that the player only picks up the objects that are tagged as “Pickups”. The pickups are classified as triggers and the player is the collider. This code tell the computer that when the Collider or player comes in contact with a trigger classified as Pickup then that object disappears.

Line 1: OnTriggerEnter gets called when the collider comes in contact with the trigger. It gets sent to the trigger Collider, and Rigidbody that the trigger collider belongs to.

Line 2: The line if (other.gameobject.CompareTag) helps tell whether or not the game object the player is in contact with has been tagged as a pickup object. If it has not then the next line does not work, but if it has then the next line of code does work.

Line 3: After the object that the player contacts has been determined as a pickup then the SetActive (false); comes into play. This line of code simply deactivates the object after the collider comes in contact with it. Unlike in the original code in which the object was destroyed and removed from the game.

Line 4: count = count +1; In the start function count was set to zero. This specific line of code is telling the computer that each time a pickup is deactivated using the pervious code then it would add 1 to the base value of count.

Line 5: SetCountText(); This function is a call to the line of code which was countText.text = “Count: “ + count.TooString (); in the void SetCountText(). This line calls upon this code to help display the amount of pickups that have been deactivated. The function is used as it is simpler, and cleaner to use rather than have the countText line of code repeated throughout the script.

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade