- Home /
OnTriggerEnter2d not working
I'm creating collectibles for in my 2d platformer, but something's wrong. I have added a circle collider to my collectible and I checked "Is Trigger." I added a javascript to my collectible but something is still going wrong. I don't get any errors or anything, my player just runs straight through the collectible. Here's my script:
function OnTriggerEnter2d (info: Collider)
{
if (info.name == "player") {
Destroy(gameObject);
}
}
$$anonymous$$ake sure your GameObject's name is exactly that "player" with a common p. $$anonymous$$ake sure your player also has a collider on. Info has to call on "Collider2D" not "Collider"
Thanks for that, I overlooked it but my player still goes straight through my coin.
Does your Collectable or the Player have a RigidBody2D attached? If one of them has, is "Is$$anonymous$$inematic" may checked?
And another Tip, don't look through the names of the GameObjects but ins$$anonymous$$d check the Tag of an Object, like info.gameObject.tag == "Player"
yeah, I have the is$$anonymous$$inematic checked on my collectable
Then uncheck it and set the Gravity Scale to 0. The option is in the same menu as the is$$anonymous$$inematic.
Answer by I_hate_c_sharp · Jul 01, 2014 at 01:55 PM
Hey I found the problem. I was using a script I hadn't looked into for some time, and it changed is kinematic. I changed that and yay! It works! I'm very sorry to everyone who spent time on this post :( stupid me
Answer by Pyrian · May 11, 2014 at 08:46 PM
OnTriggerEnter2D needs that D at the end to be capitalized.
Thank you for this. I tried an embarrassing amount of other things before finding your simple solution haha.
Answer by spiralfire11 · May 13, 2014 at 09:08 PM
Usually, i use this script when it comes to collision :
using UnityEngine; using System.Collections;
public class Collision : MonoBehaviour { void OnTriggerEnter(Collider other) {
if(other.tag == "Bullet") { Destroy (gameObject); Destroy(other.gameObject); } else { Destroy(other.gameObject);
Application.LoadLevel ("death"); } }
}
Your answer
Follow this Question
Related Questions
Making the players head face toward the location of the mouse? 1 Answer
Mouse Click to keyCode 1 Answer
2D Animation with JavaScript (Android). Help! 0 Answers
[JavaScript] Making an image sprite change upon button click? 1 Answer
How do you AddForce to a rigidbody2D in the direction of a joystick? 0 Answers