• 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 johnmadden1989 · Aug 19, 2014 at 01:03 PM · collisionprefabcolliders

Coin collecting with prefabs

Hi all,

In a level of a game I'm currently making, the player (w$$anonymous$$ch is a prefab object) has to collect coins (prefabs also). The script I'm trying to get working will ideally destroy the coin on collision. Basic enough.

However, the current code I'm using will (when altered) destroy the player on impact, but not the coin (when I change it back to read 'Coin' instead of 'Player'; t$$anonymous$$s was merely to see if it was working).

Anyone know why the devil t$$anonymous$$s'd be happening? I can't find out, no matter what boxes I tick/untick.. it just won't work. Is it a double prefab t$$anonymous$$ng?

Thanks again, any suggestions welcome.

 using UnityEngine;
 using System.Collections;
 
 public class AsteroidCollision : MonoBehaviour
 {
     void OnCollisionEnter(Collision other)
     {
         if(other.gameObject.name == "Coin")
         {
             Debug.Log("Congratulations - You Collected 1 / 10 Coins!");
             Destroy(other.gameObject);
         }
     }
 }
 
Comment
Add comment · Show 15
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 Scribe · Aug 19, 2014 at 01:08 PM 0
Share

This script is attached to the player?

avatar image johnmadden1989 · Aug 19, 2014 at 01:31 PM 0
Share

I've tried attaching it to both, but to no avail. Would you recommend attaching this to the player?

avatar image johnmadden1989 · Aug 19, 2014 at 01:35 PM 0
Share

OK, I've tried a few other collider combinations, and I've gotten it to a stage where the player passes through the coin with no destruction, or physical interference from the coin. Its like the player is a ghost..

Does this help shed any more light on the problem?

avatar image Scribe · Aug 19, 2014 at 01:50 PM 0
Share

Can you try adding a Debug to your OnCollision method to see see what's going on:

 void OnCollisionEnter(Collision other){
     Debug.Log(other.gameObject.name);
     Debug.Log(gameObject.name);
 }
avatar image johnmadden1989 · Aug 19, 2014 at 01:58 PM 0
Share

I added the Debug to it - nothing coming up in the console.. but I messed around with the Is Trigger thing again - its either the coin passes through the player, or it knocks the player object completely off course. I have a (nearly) identical script for Application.Quit on collision that works perfectly. NO idea why this is being such a pain!

Show more comments

2 Replies

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

Answer by johnmadden1989 · Aug 19, 2014 at 03:44 PM

AH!!! Thanks a bunch Scribe you absolute beast! The tag trick worked fine! That must've been it. You're an absolute legend man.

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 Scribe · Aug 19, 2014 at 03:52 PM 0
Share

Glad you got it working! Good luck with the project :)

avatar image
0

Answer by RedDevil · Aug 19, 2014 at 01:26 PM

Does the coin have a collider set with IsTrigger?

Comment
Add comment · Show 2 · 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 johnmadden1989 · Aug 19, 2014 at 01:30 PM 0
Share

Nope, I've never used that style of collider before. Do you mean in the code, or have I ticked the box that says 'Is Trigger'?

avatar image screenname_taken · Aug 19, 2014 at 03:37 PM 0
Share

The tick box. and only attach the script to one object. If you attach it to the coin for example, have the script check if the incoming collision is the player. And do its stuff.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Javascript Detecting a prefab's child's collision 2 Answers

GameObject not colliding while grabbed in VR 2 Answers

Instantiated object not colliding when not have a rigidbody?? 2 Answers

Flying Controls Collision Issues 0 Answers

Issues with instantiated prefabs 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