• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dkenne38 · Nov 26, 2012 at 11:16 PM · pointpointscollectcollection

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.

  • would really appreciate some help as it's for my coursework and i'm really stuck and how to do this :(*

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hvilela · Nov 26, 2012 at 11:25 PM

http://docs.unity3d.com/Documentation//ScriptReference/Collider.OnTriggerEnter.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

Answer by aaronpaulina · Nov 27, 2012 at 12:06 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dkenne38 · Nov 27, 2012 at 05:43 PM 0
Share

Thanks a lot guys! i'll give that code a wee try then :)

avatar image
0

Answer by Alig · Mar 22, 2013 at 06:14 AM

Can you help please with the same the but, I have a plane and I want to collect a gem through a spiral

thank so much................

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by RadicalGuy · Mar 12, 2020 at 07:24 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collestion of objects 3 Answers

Problem About Gaining Points 1 Answer

Non stopping point - counter 1 Answer

Point system, something wrong 1 Answer

score not updating 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges