How To Add Collectables In Unity: Best Guide

Introduction:

Collectables in unity are a common way to make a game more interesting and dynamic while also improving user experience and introducing a bit of difficulty. The process of adding collectibles in Unity 2D platformer will be covered in this tutorial, with special attention paid to the term unity collectable and collectables in unity.

Collectables In Unity

Implement Collectables In Unity:

1. Create a New Game Object with Sprite Image:

Begin by creating a new Unity game object and assigning a sprite picture to the sprite renderer. For example, we will utilize an apple as a collectible item. This can be done by dragging and dropping the sprite picture onto the game object to make your collectables in unity.

Add Sprite Here

2. Add Box Collider 2D:

To make the collected item interactable, include a Box Collider 2D component. Adjust the collider size as needed and enable “is trigger” to ensure that it is unaffected by physics and allows the player to pass through.

Remember To Toggle is trigger On

3. Create a Custom Tag:

Create a new tag for each collectible object to help you identify and count them. For this example, let’s call it “apple.” Assign this tag to the corresponding game item. This unique marking enables precise item counting.

First click here add tag and make a new tag.

Click here to add tag.

And then assign that tag to the apple.

Click it to assign tag.

4. Implement ObjectCollector Script:

Create a new script, name it “ObjectCollector,” and attach it to the player. Declare a variable inside the script, say “appleCount.” To identify the collectible item in the OnTriggerEnter2D method, use the if condition (collision.gameObject.CompareTag(“Cherry”)). Upon collision, destroy the item (Destroy(collision.gameObject)) and increase the apple count (appleCount++).

private int appleCount = 0;

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("apple"))
    {
        Destroy(collision.gameObject);
        appleCount++;
    }
}

5. Create a Prefab:

Create a prefab of the cherry collectible. This allows you to easily set it anywhere in the game where you want it to be gathered. The prefab ensures that the collected object has a consistent appearance and behavior.

By the way, you can read this article to implement ground check while programming the jump function in a platformer controller.

6. Use PlayerPrefs to Save Item Count (Optional):

To save the collected item count, you can utilize PlayerPrefs to make collectables in unity. The line increments the cherry count.

private int appleCount = 0;

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("apple"))
    {
        Destroy(collision.gameObject);
        appleCount++;
        PlayerPrefs.SetInt("apple", PlayerPrefs.GetInt("apple", 0) + 1);
    }
}

Let’s break down the line:

  • "apple": This is the key under which the cherry count is stored in PlayerPrefs.
  • PlayerPrefs.GetInt("apple", 0) + 1: This retrieves the current cherry count stored in PlayerPrefs. The second parameter (0) is the default value if no cherry count is found. The count is then incremented by 1.

How to access a saved variable?

Now, if you want to display the saved count, use the get function like this:

PlayerPrefs.GetInt("apple", 0) + 1);

Learn more about player prefs from here:

You Might Ask:

Why are collectables important in a Unity 2D platformer?

Collectables add an engaging element, enhancing gameplay by providing challenges and rewards for players.

Can I use different sprites for collectables in Unity?

Absolutely! You may add variety to your game by customizing your collectibles with different sprite graphics.

Why use specific tags for each collectable instead of a general tag like ‘collectable’?

Specific tags, such as ‘apple,’ allow precise identification and counting of each unique collectable item in the game.

How does the ObjectCollector script work in Unity?

The script is responsible for tracking and incrementing the count of collected items when the player interacts with them.

Is saving item count with PlayerPrefs necessary, and how does it benefit the game?

Saving item count with PlayerPrefs allows for persistent storage, letting players track their progress even after exiting the game.

Conclusion:

By following these steps, you now know how to use unity collectables and how to add collectables in a Unity 2D platformer, providing players with an engaging and rewarding gaming experience. Stay tuned for the next article, where we’ll explore creating a UI in Unity to display the collected item count to the player. Stay tuned for the future article, in which we’ll look at how to create a Unity UI that displays the player’s collected item count.