Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TheRichardGamer · Apr 14, 2014 at 12:38 PM · 2dcollisionplatformer

Why does my OnCollisionEnter2D not work?

So, I'm trying to implement power ups in my game and they work well and all, but when I collide with my power ups they just move like I'm pushing them, even though I have OnCollisionEnter2D in the script, it doesn't work, and the reason why I have it with collision and not trigger is because I don't want them to be floating in the air, meaning that they have a rigid body and that they have gravity applied to them. Why does this not work?

 var ScoreToGive : int;
 
 var PowerUpName : String;
 
 var PlayerScript;
 
 function Start () {
 
 var Player = GameObject.FindGameObjectWithTag("Player");
 
 PlayerScript = Player.GetComponent(InfiniteScore);
 
 }
 
 function OnCollisonEnter2D (Other : Collider2D) {
 
 if(Other.gameObject.tag == "Player"){
 
 PlayerScript.CurrentScore += ScoreToGive;
 Debug.Log("The player acquired a/n " + PowerUpName);
 Destroy (this.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
2

Answer by r005t · May 16, 2014 at 07:52 PM

I believe the parameter to OnCollisionEnter2D should be of type Collision2D not Collider2D.

From Unity Script Reference:

  void OnCollisionEnter2D(Collision2D coll) {
         if (coll.gameObject.tag == "Enemy")
             coll.gameObject.SendMessage("ApplyDamage", 10);
         
     }
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 TrevorP · May 16, 2014 at 08:29 PM

There's a few things you have to do:

  1. The PowerUps needs to be on a different layer than the player and then go to Edit->Project Settings -> Physics2D to make sure they don't push each other around.

  2. Next just add another collider to your PowerUp and set it to IsTrigger

  3. Last, add a OnTriggerEnter2D(Other : Collider2D) to your script (instead of OnCollisionEnter2D)

That should do it

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 richardgengle · Aug 07, 2020 at 04:14 AM

make sure you dont have collider scripts on both objects,,, unity seems to chose the simpler one,,, the one that hasnt had any varibles modified...

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

23 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

Related Questions

Having problems with 2d collision triggers (javascript) 1 Answer

BoxCast, OverlapArea, and Raycasting; ground detection questions 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Make Player Move Slow Over Grass 1 Answer

How to do a raycast to make my player unable to move through certain objects? 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