2D Coin Collecting in Unity
Coin picking and collecting have become a staple in 2D games, especially in 2D Platformers.
To pick up a coin in Unity we'll need to create a script that will be attached to a coin object and will get destroyed once the player comes in contact with it, updating the counter value.
I will be using the 2D Character Controller, but you can skip this part if you already have a 2D controller.
Steps
To make a 2D coin that can be picked up and collected, follow the steps below:
- Create a new GameObject (GameObject -> Create Empty) and name it "Coin"
- Attach a SpriteRenderer component to the "Coin" object
- Assign your coin sprite to the SpriteRenderer (you can use the image below, make sure the Texture Type in Import Settings is set to 'Sprite (2D and UI)')
- Scale the Coin object until the desired size
- Change the Coin 'Z' axis position so it matches the player's position
- Attach a BoxCollider2D component to the "Coin" object
- Create a new script, call it "SC_2DCoin", remove everything from it then paste the code below inside it:
SC_2DCoin.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SC_2DCoin : MonoBehaviour
{
//Keep track of total picked coins (Since the value is static, it can be accessed at "SC_2DCoin.totalCoins" from any script)
public static int totalCoins = 0;
void Awake()
{
//Make Collider2D as trigger
GetComponent<Collider2D>().isTrigger = true;
}
void OnTriggerEnter2D(Collider2D c2d)
{
//Destroy the coin if Object tagged Player comes in contact with it
if (c2d.CompareTag("Player"))
{
//Add coin to counter
totalCoins++;
//Test: Print total number of coins
Debug.Log("You currently have " + SC_2DCoin.totalCoins + " Coins.");
//Destroy coin
Destroy(gameObject);
}
}
}
- Attach the SC_2DCoin script to the "Coin" object
- Select your player object and make sure its tag is set to "Player" (this is needed to be able to pick up the coin)
The coin is now ready, you can save it to Prefab and duplicate it around the level.
To create a coin counter, follow the steps below:
- Create a new UI Image by right-clicking on the Hierarchy view -> UI -> Image and name it "CoinIcon"
- Assign coin sprite to the Image component
- Change RectTransform Alignment to 'top left', Pivot to (0, 1), Post X to '5', Pos Y to '-5', Width and Height to '25'
- Create new Text by right-clicking on the Hierarchy view -> UI -> Text and name it "CoinCounter"
- Set "CoinCounter" RectTransform the same as "CoinIcon", except set Pos X to '35' and Width to '160'
- Set Text Font Style to 'Bold', Font Size to 22, Alignment to 'left center' and Color to 'white'
- Create a new script, call it "SC_CoinCounter", remove everything from it then paste the code below inside it:
The coin counter script will apply the number of coins to the Text element.
SC_CoinCounter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SC_CoinCounter : MonoBehaviour
{
Text counterText;
// Start is called before the first frame update
void Start()
{
counterText = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
//Set the current number of coins to display
if(counterText.text != SC_2DCoin.totalCoins.ToString())
{
counterText.text = SC_2DCoin.totalCoins.ToString();
}
}
}
- Attach the SC_CoinCounter script to the "CoinCounter" Text object
Press Play and observe the coins disappear on player contact and are added to a counter.