Unity PowerUp pickup

Create a Sprite object with BoxCollider2D (set isTrigger true) and Rigidbody2D. Create a new Script, name it PowerUp.

using UnityEngine;

public class PowerUp : MonoBehaviour
{
    [SerializeField] int _powerUp;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Hp hp = collision.gameObject.GetComponent<Hp>();
        if (hp != null)
        {
            hp.AddHealth(_powerUp);
            Destroy(gameObject);
        }
        
    }
}

Whenever other object that has Hp script on it (Rigidbody2D + Collider2D) will collide with PowerUp object, hp will be increased by the amount specified in PowerUp object.

Scroll to Top