How to make a character collect items?

I’ve basically never used Unity so i have no clue how to do this.

I want my character to collect the ‘gems’ and for each gem that is collected the player gets a point. Simple as that.

All i have set up just now is my terrain with the character walking about and the gems on the terrain.

I would really appreciate some help as it’s for my coursework and i’m really stuck and how to do this :frowning:

Save this as “SmartCollider.cs” and drag it onto your player object…

`using UnityEngine;
using System.Collections;

public class SmartCollide : MonoBehaviour {

//GameObject you might collide with 
public Collider CollideWith;

//if you'd like to call a function on yourself when you collide with CollideWith
public string MessageToSelf;

//calls a function on the collider object
public string MessageToCollider;

//If you want to call a function on yourself when they collide.
public bool SendToSelf = false;

public string OnCollisionEnterMessage, OnCollisionExitMessage, OnCollisionStayMessage,
    OnTriggerEnterMessage, OnTriggerExitMessage, OnTriggerStayMessage;

void OnCollisionEnter(Collision collision)
{
    if (collision.collider == CollideWith)
    {
        CollideWith.SendMessage(OnCollisionEnterMessage, SendMessageOptions.DontRequireReceiver);
        if (SendToSelf == true)
        {
            this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
        }
    }
}

void OnCollisionExit(Collision collision)
{
    if (collision.collider == CollideWith)
    {
        CollideWith.SendMessage(OnCollisionExitMessage, SendMessageOptions.DontRequireReceiver);

        if (SendToSelf == true)
        {
            this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
        }

    }
}

void OnTriggerEnter(Collider collision)
{
    if (collision.collider == CollideWith)
    {
        CollideWith.SendMessage(OnTriggerEnterMessage, SendMessageOptions.DontRequireReceiver);

        if (SendToSelf == true)
        {
            this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
        }

    }
}

void OnTriggerExit(Collider collision)
{
    if (collision.collider == CollideWith)
    {
        CollideWith.SendMessage(OnTriggerExitMessage, SendMessageOptions.DontRequireReceiver);

        if (SendToSelf == true)
        {
            this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
        }

    }
}

void OnTriggerStay(Collider collision)
{
    if (collision.collider == CollideWith)
    {
        CollideWith.SendMessage(OnTriggerStayMessage, SendMessageOptions.DontRequireReceiver);

        if (SendToSelf == true)
        {
            this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
        }

    }
}

}`

Now just create a function on your Gem script
public void AddGem() { OurGemsVariable += 1; }

drag your gem object onto the Smart collide scipt where it says “CollideWith” and under the OnCollisionEnterMessage type in " AddGem "

now when your player runs into the gem it should add +1 to your gems :slight_smile:

You can do this easy!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Collectable : MonoBehaviour
{

 private void OnTriggerEnter(Collider other) 
 {
     if (other.tag == "Player")
    {
        //add points
        //add ability
        //add any powerup here
        Destroy(this.gameObject);
    }
 }
}

make sure to add a box collider with trigger enabled

and make sure your player is tagged Player