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 /
  • Help Room /
avatar image
0
Question by Vyxis · Feb 29, 2016 at 12:16 PM · c#collisiontriggerunity5triggers

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);
     }
 }

Comment
Add comment · Show 2
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 Ali-hatem · Feb 29, 2016 at 05:12 PM 0
Share

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"") 
avatar image Vyxis · Feb 29, 2016 at 05:16 PM 0
Share

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

1 Reply

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

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....

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 Ali-hatem · Feb 29, 2016 at 05:28 PM 0
Share

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Activating OnTriggerEnter when changing the collider to trigger? 1 Answer

OnTriggerEnter not firing 2 Answers

How can I remove movement or camera rotation in one direction with a trigger 0 Answers

I want to freeze fp player , animation then starts , then unfreeze the character . 0 Answers

Applying force when colliding with several objects 1 Answer


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