PickUp PreFabs
Throughout our game we will want to have game items for the player to interact with. Often these are considered Pick-up items.
Create / Configure PickUp PreFab GameObject
In Unity, we'll want to create and configure gameObjects that will have this script attached to give desired behavior. You are required to have 4 types of PickUp objects in your game, 3 must have tag: Collectible, 1 can have tag: Hazard. See additional information - Create 2D Sprite Prefab: Rock
Create a 2D Sprite GameObject
Add a Collider2D to the GameObject, configure collider shape
Configure the Collider2D to be isTrigger == true
Select a tag for the gameObject: "Collectible", "Hazard", other?
Add Rigidbody2D if the gameObject will move
Optional add additional Collider2D inside trigger Collider2D, to keep this gameObject from falling through the floor's Collider2D.
Add PickUp script
Select type from dropdown in inspector
Set value, set a positive value
Drag into Project Assets Resource Folder to create as a prefab
PickUp Class - v1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PickupType
{
crate, crystal, rock, heart //add as needed
}
public class PickUp : MonoBehaviour
{
[SerializeField]
private int item_value=0;
//property for item_value updating health, score
public int Value
{
get
{
return item_value;
}
set
{
value = item_value;
}
}
public PickupType type; //what is the PickupType of this object
}// end class
Last updated
Was this helpful?