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 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.
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
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).
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