Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ragefordragons · Jun 16, 2017 at 11:22 PM · not workingcollision2d

My 2D collision wont work?

So I am trying to create a coin that the player can pick up and I'm pretty new to unity 2D. For some reason even though my character has a box collider and a rigid body, and the coin has a rigid body, it wont collide! Is the issue my code?

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class coinpickup : MonoBehaviour {

 public float coins = 0;
 public GameObject coin; 

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {

     
 }
 void OnTrigger2DEnter(Collider2D col)
     {

     coins = coins + 1;
     Destroy(coin.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

3 Replies

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

Answer by heskey30 · Jun 17, 2017 at 12:00 AM

The correct function name is OnTriggerEnter2D - you could just google it and find the documentation.

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 ragefordragons · Jun 17, 2017 at 03:32 AM 0
Share

Hey thanks so much! That seemed to fix it!

avatar image
1

Answer by zergnick · Jun 17, 2017 at 01:11 AM

Did you add a collider to the coin? Normally I will not use a rigid body for the coin if I only need to pick it up. Also, if you use OnTriggerEnter2D, you need check the 'Is Trigger' in the Coin's collider. Otherwise, you could use OnCollisionEnter2D.

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 JxWolfe · Jun 17, 2017 at 01:15 AM 0
Share

assu$$anonymous$$g that you are using 2d colliders and physics

avatar image
0

Answer by JxWolfe · Jun 17, 2017 at 01:04 AM

The box Collider has to have isTrigger set to true, and it sounds like you are using 3d Colliders and Physics... did you mean boxCollider2d and Rigidbody2d? otherwise you would use:

 void OnTriggerEnter(Collider other)
     {
      coins=coins+1;
      Destroy(coin.gameObject);
     }

That would be my idea, and you should have the coins public static or on the 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.

Follow this Question

Answers Answers and Comments

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

Related Questions

[SOLVED] How to Destroy a rigidbody game object on collision with another gameobject? 4 Answers

I have been trying to get a working isGrounded variable to get my character to stop from infinitely jumping can someone please show and/or explain what it is that I am doing wrong. 1 Answer

IAP doesn't work in google play store (on device) but works fine in the editor. 0 Answers

Unity 2018 "Not Responding" in task manager when I try to make a build. 0 Answers

2d racing play button not working 2 Answers


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