---
title: "Create a rotating collectible"
content_type: tutorial
url: "https://learn.unity.com/pathway/unity-essentials/unit/programming-essentials/tutorial/create-a-rotating-collectible?language=en"
language: en
container_type: pathway
container_title: "Unity Essentials"
container_url: "https://learn.unity.com/pathway/unity-essentials.md?language=en&version=6.3"
unit_title: "Programming Essentials"
unit_url: "https://learn.unity.com/pathway/unity-essentials/unit/programming-essentials?language=en&version=6.3"
standalone_markdown_url: "https://learn.unity.com/tutorial/create-a-rotating-collectible.md?language=en&version=6.3"
previous_title: "Add a movement script"
previous_url: "https://learn.unity.com/pathway/unity-essentials/unit/programming-essentials/tutorial/add-a-movement-script?language=en&version=6.3"
previous_markdown_url: "https://learn.unity.com/pathway/unity-essentials/unit/programming-essentials/tutorial/add-a-movement-script.md?language=en&version=6.3"
next_title: "Collect the collectible"
next_url: "https://learn.unity.com/pathway/unity-essentials/unit/programming-essentials/tutorial/collect-the-collectible?language=en&version=6.3"
next_markdown_url: "https://learn.unity.com/pathway/unity-essentials/unit/programming-essentials/tutorial/collect-the-collectible.md?language=en&version=6.3"
difficulty: foundational
duration_minutes: 25
unity_version:
  - "6.0"
  - "6.3"
supported_unity_versions:
  - name: "2022.3"
    url: "https://learn.unity.com/pathway/unity-essentials/unit/programming-essentials/tutorial/create-a-rotating-collectible.md?language=en&version=2022.3"
  - name: "6.0"
    url: "https://learn.unity.com/pathway/unity-essentials/unit/programming-essentials/tutorial/create-a-rotating-collectible.md?language=en&version=6.0"
  - name: "6.3"
    url: "https://learn.unity.com/pathway/unity-essentials/unit/programming-essentials/tutorial/create-a-rotating-collectible.md?language=en&version=6.3"
last_updated: "2026-08-04T14:23:58.000Z"
---

# Create a rotating collectible

Write your first C# line from scratch to make a collectible object rotate around its Y-axis.

Program your first line of code from scratch to make your collectible object rotate around its Y-axis.

## Overview

[Video](https://connect-mediagw.unity.com/h1/20240515/videos/ef2fe4af-91d1-44db-9ae4-fcb8de2ab09b_4.2.1_-_Overview_-_VIDEO.mp4)

In the last tutorial, you copy and pasted code into a new script, which is a great way to start. In this tutorial, you’ll write your first line of code to make a collectible object rotate in the scene. These rotating collectible objects will be the things that your player character tries to gather throughout the living room scene.

When you’re ready to get started, proceed to the next step.

## Add a collectible GameObject in front of the player

[Video](https://connect-mediagw.unity.com/h1/20240515/videos/5c27fdf2-6c10-4543-96e0-365d59718692_4.2.2_-_Add_a_collectible_GameObject_in_front_of_the_player_-_VIDEO.mp4)

The first step is adding a collectible item in front of the player. You'll probably want to select a collectible prefab that makes sense with the character you've chosen:

- The robot vacuum could suck up dirt
- The toy car could get little stars
- The wind-up mouse could find cheese
- The alien UFO could collect precious gemstones.

There are also a bunch of other interesting collectibles to choose from, so feel free to get creative with your selection.


**Instructions**

**1.**  In the **Project** window, navigate to **\_Unity Essentials** > **Prefabs** > **Collectibles.**

**2.**  Drag and drop your chosen collectible prefab into the scene directly in front of the player.

**3.**  Adjust the collectible’s position, rotation, and scale in the **Scene** view as needed to ensure that it's easily visible and reachable by the player.

This will make testing as efficient as possible.

**Tip:**  You might want to adjust the position and rotation of the camera so that you can easily see the collectibles in front of the player in **Play** mode.

## Create and apply a new Collectible script

[Video](https://connect-mediagw.unity.com/h1/20240719/videos/9649cccb-6ff7-4ff0-a367-9c75edff56d5_4.2.3_-_Create_and_apply_a_new_Collectible_script_-_VIDEO_U6_V2.mp4)

In this step, you’ll create a new empty script. This script, which you’ll name "Collectible", will govern the behavior of the collectible objects you've added to the scene.


**Instructions**

**1.**  In the **Project** window, navigate to the **\_Unity Essentials** > **Scripts** folder you accessed in the previous tutorial.

**2.**  Within the **Scripts** folder, right-click and select **Create** > **MonoBehaviour Script.**

**3.**  With the script’s name automatically highlighted, immediately rename this new script "Collectible".

Just like last time, the spelling of the name is important because it becomes part of the script.
**4.**  Press **Enter** to save the script name.

You'll see the code of the new blank script in the **Inspector** window.

**Note:**  If you make a typo in your script name or have to rename it for any reason, it will be easier to just delete it and create a new one with the correct name.

**5.**  In the **Hierarchy**, select the **Collectible** GameObject.

**6.**  Drag and drop the **Collectible** script you just created from the **Project** window into an empty space of the **Inspector** window for the **Collectible** GameObject (such as the space around the **Add Component** button).

Note that this is different from the technique you used earlier to apply a script, where you dragged it onto the object directly in the **Scene** view. There are usually a few different ways to do the same things in Unity!

## Review the structure of the default script

![](https://connect-mediagw.unity.com/h1/20240515/learn/images/a92c6f7c-86f3-488a-b146-016d1346cc95_4.2.4_-_Review_the_structure_of_the_default_script.png)

Before you start coding, take a minute to review the structure of the default script you've just created.

This might feel overwhelming at first, especially if you're new to programming. It's important to remember that understanding the nuances of scripting is a gradual process and it’s entirely normal to feel a bit daunted at this point! So don’t worry about that and trust that it will all slowly start to make sense over time.


**Instructions**

**1.**  In the **Project** window, double-click the **Collectible** script to open it in the script editor.

**2.**  Locate the **using statements** at the top of the script, including “using UnityEngine;”.

These lines link the script to the necessary libraries so that when you enter something like “Rigidbody,” the script knows what that means.

**3.**  Beneath the using statements, locate the class declaration:
**public class Collectible : MonoBehaviour.**

This defines the script as a public class named "Collectible" that inherits from Unity's **MonoBehaviour** class. **MonoBehaviour** is a required class for all GameObjects in Unity. This bit of code is what allows you to add your script as a component to a GameObject.

Don’t worry if this is still confusing at this point — it will slowly make more and more sense as you continue on.

**4.**  Locate the **Start() function.**

This function is executed once at the beginning of the game.

**5.**  Locate the **Update() function.**

Unity runs this function once per frame.

Games typically run at 30-60 frames per second, so any code within this function will be executed many times continuously throughout the game, making it suitable for ongoing actions and animations. This is where you’ll put your rotation code so that the collectible will rotate continuously.

## Plan your rotation code

[Video](https://connect-mediagw.unity.com/h1/20240515/videos/bb14e390-2446-41c2-bfb8-e85950079abf_4.2.5_-_Plan_your_rotation_code_-_VIDEO.mp4)

Before writing your first line of code, let’s take a moment to understand what you’re actually trying to do in your code. You’ll experiment in the **Scene** view to visualize this.


**Instructions**

**1.**  Select the collectible object and frame it in the **Scene** view so you have a good view of it.

**2.**  At the top of the **Scene** view, make sure you’re using the **Local** coordinate system instead of the **Global** system so that you’ll be able to see the orientation of the collectible changing as it rotates.

**3.**  In the **Inspector** window, in the **Transform** component, locate the **Rotation** coordinates. Click and drag the label **Y** right and left to change the Y-axis rotation.

Watch the demo video again very closely if you’re still not sure how to do this.

**Tip:**  If you hold down the **Shift** key while you click and drag the value, the value will change more quickly.

Observe how the object rotates in the **Scene** view. This visual experiment helps you see the effect of rotation values on your object. This is the effect you want to achieve in your script: you want to increase the Y rotation value in the **Transform** component continuously over time.

In the next step, you’ll add the code that rotates the collectible around the Y-axis continuously.

## Add your first line of code to rotate the collectible

[Video](https://connect-mediagw.unity.com/h1/20240515/videos/389538f2-c50a-4dd7-a892-637a700c24cc_4.2.6_-_Add_your_first_line_of_code_to_rotate_the_collectible_-_VIDEO.mp4)

It's time to add continuous rotation from your **Collectible** script. You'll do this by manipulating the object's **Transform** component, which controls the object's position, rotation, and scale in the scene.

Specifically, you'll use the **transform.Rotate** method, a built-in Unity function that makes an object rotate in three dimensional space. For a simple rotation effect, you'll change the Y value, which makes the object rotate around its vertical axis.

When you put this rotation code in the **Update** function, the collectible will rotate a little bit each frame, making it appear to smoothly, continuously rotate.


**Instructions**

**1.**  Open the **Collectible** script in the script editor.

**2.**  Put your cursor inside the **Update()** method, between the sets of curly brackets.

It can be helpful to add a few new lines using the **Enter** (macOS: **Return**) key to give yourself a bit of space before you add your new code.

The extra space won’t change the functionality of the code, but it can make your code easier to read.

**3.**  Write the following line of code inside the **Update** function, between the opening and closing curly brackets:

 **transform.Rotate(0, 1, 0);**

**Important:** Don’t forget to add the semicolon at the end of the line of code.

This line instructs Unity to rotate the collectible object 1 degree around the Y-axis in each frame, creating a continuous rotation effect.

**4.**  Save the script by pressing **Ctrl+S** (macOS: **Cmd+S**) or by selecting **File** > **Save** from the main menu of the script editor.

**5.**  Back in the Unity Editor, enter Play mode to see the collectible object rotating continuously.

**Tip:** During **Play** mode, you can switch to the **Scene** view tab, which will allow you to get a close-up view of your collectible as the application is running.

**6.**  Remember to exit **Play** mode when you’re done testing!

**Note:** If IntelliSense in Visual Studio doesn’t suggest code, open **Window** > **Package Management** > **Package Manager**, set the source to Unity Registry, and ensure the “Visual Studio Editor” package is installed (reinstall it if needed).

## Reduce speed to a decimal float value

[Video](https://connect-mediagw.unity.com/h1/20240515/videos/92a51f95-29be-4346-968d-713b3ed8d963_4.2.7_-_Reduce_speed_to_a_decimal_float_value_-_VIDEO.mp4)

You might notice that the collectible rotates really quickly. However, if you try to reduce the value from **1** to **0.5** in your script, you'll get an error in your code, indicated by a red underline.

In C#, decimal numbers are treated as **float values.**
A float, short for "floating point number", is a data type that can handle decimals.

**Data types** in programming specify the type of a value and how the computer interprets this value. For instance, integers for whole numbers, floats for decimal numbers, and strings for text.

When you use float values in C#, you need to explicitly indicate it's a float by putting an “f” at the end of the number.


**Instructions**

**1.**  Reopen the **Collectible** script in your script editor.

**2.**  Modify the rotation line in the **Update()** method to **transform.Rotate(0, 0.5f, 0);.**

This change reduces the rotation speed to half a degree per frame.

When entering the decimal value **0.5,** make sure to add “f” at the end (0.5f). Try temporarily removing the “f” to see the error in your code.

**3.**  Save the script and return to the Unity Editor. Play your scene to observe the slower rotation of the collectible.

## Declare new rotation speed variable

[Video](https://connect-mediagw.unity.com/h1/20240515/videos/8847b696-af24-47dd-9e10-42ea3c0de85b_4.2.8_-_Declare_new_rotation_speed_variable_-_VIDEO.mp4)

In this step, you will enhance the flexibility of the rotation behavior by introducing a **public variable** to your script.

A variable in programming is just a named piece of data that can vary. When you declare a public variable for rotation speed, you'll allow yourself, and anyone else who works on this project in Unity, to easily adjust the rotation rate directly from the **Inspector** window without the need to modify the script.

This practice is an example of efficient game programming.


**Instructions**

**1.**  Open the **Collectible** script in your script editor.

**2.**  Above the **Start()** method, but after the first opening curly bracket of your script, use the **Enter** (macOS: **Return**) key to add a few empty lines to make some space for yourself to type.

**Important:**  Pay close attention to the demo video to see where to put this code. Putting it in the wrong section of your script will cause errors.

**3.**  Declare a new public float variable before the **Start** method by adding the line:

 **public float rotationSpeed;**

This creates a public variable named **rotationSpeed** of type float. The word "public" (as opposed to "private") makes this value editable in the **Inspector** window.

Using a lowercase letter for the first word, but uppercase letters at the start of all subsequent words is known as **camelCase,** and is a common practice when naming variables. It’s called camelCase because it kind of looks like a camel from the side, with humps at the center of the word.

**4.**  Now, replace the hard-coded rotation value in the **Update()** method with this new variable you just created:

   **transform.Rotate(0, rotationSpeed, 0);**

**5.**  Save the script and switch back to the Unity Editor. In the **Inspector** window, you'll now see the **rotationSpeed** variable in the **Inspector** window when you select the collectible object, which is set to zero by default. You’ll also notice that Unity has taken your camelCase **rotationSpeed** variable and changed it to **Rotation Speed** in the **Inspector** window for improved readability.

**6.**  In the **Inspector** window, set the **Rotation Speed** value to a number like **0.5** to control the speed of rotation. Edit the value to the rotation speed you like the best.

Now, if you ever want to adjust that value in the future, you don’t need to open a script! You can make the change right there in the **Inspector** window.

**Note:**  Remember to save your scene after exiting **Play** mode.

## Review and proceed to next tutorial

Congratulations on writing your first line of code to make the collectible rotate about the Y-axis!

Here are some of the things you learned along the way:

- Explain the **using** statements at the top of a Unity script.
- Identify a class declaration in a script.
- Explain the MonoBehaviour class in Unity scripts.
- Explain the **Start()** and **Update()** functions in a Unity script.
- Call a transform function in the **Update()** method to transform a GameObject over time.
- Apply correct data types in code, including integers and floats.
- Declare new custom variables in a script.
- Explain how public variables in a script behave in Unity.
- Describe common naming conventions including PascalCase and camelCase.
- Apply correct naming conventions to variables and methods.


**Instructions**

Proceed to the next tutorial where you'll finalize the living room functionality. You’ll add more code that will allow the player to pick up the collectible object. and then you’ll arrange more collectibles throughout the room.
