OnTriggerEnter2D not working
I'm in the mids of creating a spaceshooter and i'm getting troubles with my collision triggers.
i got 3 collisions and 1 that needs to make a trigger. these are
Asteroids (isTrigger)
bullet
spaceship
so when my asteroids hits my bullet or spaceship the astroid needs to get destroyed and same goes for my bullet.
But now comes my issue. It doesnt work.
I have troubleshooted multiple possible things and they won't work. I have made all my coliders triggers, made the rigidbodys kinematic and not and tried to see with the debug.log if they actually get triggered. This isn't the deal. They don't get triggered so i have no clue why they aren't. I even created tags even though they should get the tag from the name of the gameobject but since it's an option i careated tags for them to get recognized by.
Help me please i am out of options.
public int speed = -5;
// Use this for initialization
void Start () {
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, speed);
GetComponent<Rigidbody2D>().angularVelocity = Random.Range(-200, 200);
}
// Update is called once per frame
void Update () {
}
void onTriggerEnter2D(Collider2D obj)
{
if (obj.gameObject.tag == "bullet(Clone)")
{
Debug.Log("HIT BULLET");
Destroy(gameObject);
Destroy(obj.gameObject);
}
if(obj.gameObject.tag == "spaceship")
{
Debug.Log("HIT SPACESHIP");
Destroy(gameObject);
}
}
even though they should get the tag from the name of the gameobject !!!!
you should tag the bullet & the ship in the editor &
if(obj.tag=="") is enough not if(obj.gameObject.tag"")
No it wasn't that just check my onTriggerEnter2D fuction it self and see that on isn't written with a capital "O" ... F**me................ sorry for cursing
Answer by Vyxis · Feb 29, 2016 at 05:14 PM
Resolved... due to my basic knowledge of other programming languages i start my functions/voids with a capital letter. i didn't know C# had to start every function witha Capital... rookie mistake....
grate but if the astroid should not avoid destroing other objects you can just use
void onTriggerEnter2D(Collider2D obj)
{
Destroy(gameObject);
Destroy(obj.gameObject);
}
and avoid testing the tag of other objects its more efficient i think.