Day 122 — Creating a Loot System in Unity

public int diamonds;
public int gems = 1;private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Player player = other.GetComponent<Player>();
if (player != null)
{
player.diamonds += gems;
Destroy(this.gameObject);
}
}
}
[SerializeField]
protected GameObject _diamondPrefab;
public void Damage()
{
if (isDead == true)
{
return;
}
Health--; if (Health < 1)
{
isDead = true;
anim.SetTrigger("Death");
GameObject diamond = Instantiate(_diamondPrefab, transform.position, Quaternion.identity);
diamond.GetComponent<Diamond>().gems = base.gems;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store