Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 ashjack · Mar 30, 2014 at 09:48 AM · javascriptcollidertagpointcoin

Collider script refuses to work

I am making an Android game, and when the player touches a coin, it adds it to the score and destroys the coin object. But the code refuses to work, and I don't know why. The coin has a mesh collider on it, and I thought maybe that was the issue. So I tweaked the code to detect rocks instead, which have sphere colliders on. Still no luck.

 var PlayerCoins : int;
 var CoinText = "Coins: " + PlayerCoins;
 
 function OnTriggerEnter(other : Collider)
 {
     if(other.tag == "coin")
     {
         PlayerCoins +=1;
         CoinText = "Coins: " + PlayerCoins;
         Destroy(other.gameObject);
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by hamstar · Mar 30, 2014 at 10:28 AM

Hard to know exactly, but here's a guess.

You might need to use OnCollisionEnter...

I imagine that your Player object is not using a trigger collider, but perhaps your coin object is. Therefore, since this script is attached to the player, you should use `OnCollisionEnter`. You would use `OnTriggerEnter` if the script was attached to an object with a trigger collider.

If you are working with 2D colliders...

You should use `OnTriggerEnter2D` or `OnCollisionEnter2D`.

Make sure your coin game object definitely has the tag "coin".

Comment
Add comment · Show 8 · 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 ashjack · Mar 30, 2014 at 10:59 AM 0
Share

I've tried both, and it's still not working. I made sure I followed what I was supposed to do in the documentation.

avatar image hamstar · Mar 30, 2014 at 11:15 AM 1
Share

You could try this test code to check if any collisions are registering:

 function OnTriggerEnter(other : Collider)
 {
   Debug.Log("OnTriggerEnter - Collided with object tag: " + other.gameObject.tag);
 }
 
 function OnCollisionEnter(collision : Collision) 
 {
   Debug.Log("OnCollisionEnter - Collided with object tag: " + collision.gameObject.tag);
 }

What is the output?

avatar image ashjack · Mar 30, 2014 at 11:17 AM 0
Share

There's no output for either of them

avatar image hamstar · Mar 30, 2014 at 11:36 AM 1
Share

I'm a bit stumped then sorry. Can you try this, just to make sure?

 function Start () {
     Debug.Log("I am attached to a game object!");
     Debug.Log("$$anonymous$$y game object is, name: " + gameObject.name + " tag: " + gameObject.tag);
     Debug.Log("I have a 3D collider?: " + (collider != null));
     Debug.Log("I have a 2D collider?: " + (collider2D != null));
 }
avatar image hamstar · Mar 30, 2014 at 12:19 PM 1
Share

Does the object with the script have a rigidbody? From docs:

Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.

Show more comments

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.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can to convert it on c#? 1 Answer

Collide script not working... 1 Answer

Footstep Audio Check Collider Hitting Floor 1 Answer

Adding varibales from other scripts 3 Answers

How to block dragging if collider hits 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