Creating Collectibles

Tutorial

·

Beginner

·

+10XP

·

15 mins

·

(6009)

Unity Technologies

Creating Collectibles

In this tutorial, you’ll:

  • Create a PickUp GameObject for the player to collect
  • Write a script to rotate the collectible
  • Turn the PickUp GameObject into a Prefab
  • Instantiate the Prefab around your play area

Languages available:

1. Overview

In this tutorial, you’ll:

  • Create a PickUp GameObject for the player to collect
  • Write a script to rotate the collectible
  • Turn the PickUp GameObject into a Prefab
  • Instantiate the Prefab around your play area

By the end of this tutorial, your game will look something like this:

2. Create a collectible GameObject

Follow the video or text instructions below to create a PickUp GameObject, set the PickUp GameObject’s Transform values, and create a PickUp material:

Download Transcript

1. Create a PickUp GameObject.

  • In the Hierarchy window, right-click > 3D Object > Cube, then rename it “PickUp”.
  • Reset the PickUp GameObject’s Transform component.
  • Press the F key to frame in on the PickUp GameObject in the Scene view.
  • Use the Move tool to drag the PickUp GameObject out of the way so it’s not overlapping with the Player GameObject.

2. Set the PickUp Gameobject’s Transform values.

  • Set the PickUp Gameobject’s Position Y value to 0.5 to raise it above the Ground GameObject.
  • Set each of the Rotation axis values to 45 to tilt it on an angle.
  • Set each of the Scale axis values to 0.5 to make it smaller.

3. Create a PickUp material.

  • In the Project window, find and select the Background material.
  • In the main menu, go to Edit > Duplicate. Alternatively, you can press Ctrl+D (macOS: Cmd+D).
  • Rename the new material “PickUp”.
  • With the Pickup material still selected in the Project window, use the color picker to change its base map color property.
  • Set the RGB values 255, 200, and 0 for a bright yellow.
  • Drag the PickUp material from the Project window onto the PickUp GameObject in the Scene view.

3. Rotate the PickUp GameObject

Follow the video or text instructions below to create a new Rotator script and rotate the PickUp GameObject in the Update function:

Download Transcript

1. Create a new Rotator script.

  • With the PickUp GameObject selected in the Hierarchy window, select Add Component > New script in the Inspector window.
  • Name your new script “Rotator”.
  • In the Project window, move the script from the root Assets folder into the Scripts folder.
  • Open the new script for editing.

2. Rotate the PickUp GameObject in the Update function.

  • With the Rotator script open, remove the Start function — you won’t need it.
  • After the opening curly brace of the Update function, add the following line of code:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

4. Make PickUp a Prefab

Follow the video or text instructions below to turn the PickUp GameObject into a prefab, then enter and exit prefab editing mode:

Download Transcript

1. Turn the PickUp GameObject into a prefab.

  • In the Assets folder, right-click > Create > Folder, then rename this new folder “Prefabs”.
  • Drag the PickUp GameObject from the Hierarchy window into the Prefabs folder.
  • When prompted, select Original Prefab.

2. Enter and exit prefab editing mode.

  • Select the arrow to the right of the PickUp GameObject in the Hierarchy window to open prefab editing mode.
  • To return to the normal Scene view, select the back arrow at the top of the Hierarchy window.

5. Add more collectibles

Follow the video or text instructions below to make an empty parent GameObject for the pickups, align to a top view of the scene, place duplicate pickups around the scene, and test your game:

Download Transcript

1. Make an empty parent GameObject for the PickUp GameObjects.

  • In the Hierarchy window, create a new empty GameObject and name it “PickUp Parent”.
  • Reset the Transform component.
  • In the Hierarchy window, drag the PickUp GameObject onto the PickUp Parent GameObject.

Important: Before beginning this step, set the tool handles in the Scene view to Global coordinates rather than Local. With Global coordinates, the Scene view tools will always point in the same directions, regardless of how the selected object is rotated. This will make it easier to move the Pickups straight along the X and Z planes.

2. Align to a top view of the scene.

  • Select the Gizmo in the upper right of the Scene view to switch to a top-down view.
  • Zoom out a little so you can see the entire play area.

3. Place duplicate pickups around the scene.

  • Move the first PickUp GameObject somewhere you like.
  • With the PickUp GameObject selected, duplicate it with Ctrl+D (macOS: Cmd+D).
  • Use the Move tool to relocate the second instance of the prefab.
  • Repeat this process to place as many PickUp GameObjects as you like in the scene.

4. Test your game.

  • When you test your game, you will notice that the PickUp GameObjects are still acting like solid obstacles. You’ll fix that in the next tutorial.
Optional Step

6. Final script sample

If your script is not working as expected, below is an example of what your code should look like at this point. The comments have been added to make the code more readable.

Rotator.cs

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Complete this tutorial