- Home /
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);
}
}
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);
}
Answer by TrevorP · May 16, 2014 at 08:29 PM
There's a few things you have to do:
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.
Next just add another collider to your PowerUp and set it to IsTrigger
Last, add a OnTriggerEnter2D(Other : Collider2D) to your script (instead of OnCollisionEnter2D)
That should do it
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...
Your answer
![](https://koobas.hobune.stream/wayback/20220613142700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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