Make basic script to collect items?

Hello all! Sorry for the really stupid question but I’m new to Unity and scripting in general. What I’d like to do is I’d like to have a script that detects when a player has collected seven gems, then triggers an event.

I’ve looked at other tutorials but this is practically my first time using Unity and I can’t make heads or tails of them. I know that the gems need colliders on them that are triggers, but that’s as far as I know :stuck_out_tongue: Do I attach the script to collect gems to the player or the gems?

Any help at all would be greatly appreciated!

Also I know tags are involved somehow but I can’t get custom tags to register on objects? I create new tags but they don’t show up in the drop-down menu for an object.

Do you want to run into the items or click on them with your mouse?

Run into, ideally. The gems are just hovering.

TAGS: Look more closelly at the tags and layers menu. there is an expandable list that contains X elements, where X is the number of custom tags or layers. It should show up in the drop-down if you put your custom tags there.

PICKING UP GEMS: Yes, you need colliders on both your player and the gems, the gems being triggers. Try using the OnTriggerEnter function to check for collision, and then change a variable (preferably an integer) to symbolise it being picked up. Destroy the gem gameobject afterwards via the Destroy() function.

DETECTING HOW MANY GEMS THE PLAYER HAS is really simple – an if statement that compares the integer responsible for keeping track of the number of gems picked up with a number of your choosing (7, was it?). If the condition is satisfied, do whatever you wish – play a sound, quit application, load another level, display something :slight_smile:

On the player create a collider that is a trigger(sphere is best)
Then add this script to the player.

var itemCount : int;

function OnTriggerEnter(col : Colider) {
    if(col.tag=="TagName") {
      Destroy(col.gameObject);
      itemCount ++;
    }
}

Be sure to tag your items. After you get this part working I will help you with the next part (triggering and event when you getthe right amount of items).

2 Likes

I’ve got a collider on the player; do I need one on the gem as well?

Also I forgot to mention; the gems have been created in Maya and have been imported. The script you’ve given me doesn’t work and I think that may be why. Although I did test it by creating a gameObject sphere, making it a collider, then tagging it as a ‘gem’ and it still didn’t work :confused:

If you copied the script word for word, there is a high probability it wouldn’t :slight_smile: You need to insert the names of your own tags in the script.

Yes, both the player and the gem needs colliders. You can insert a box or sphere collider (or any other you wish) into importated objects as well.

I did insert my own tags but for some reason it’s just not registering :confused:

And thanks, I’ll put some colliders on. Don’t know why a sphere with the ‘gem’ tag didn’t get ‘picked up’ by the script :confused:

:slight_smile:

hi i am wondering if i could also get help?

i need to know if i could set up an accurate leaderboard

Please don’t multipost or reply to old threads https://discussions.unity.com/t/850835

:frowning:

I replied to your other thread, good luck on your project!