Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Faniha · May 17, 2015 at 05:50 PM · c#2dcollider

what else could be wrong, OnTriggerEnter

ok I want to use OnTriggerEnter but cannot get it to work. The script is basic with a debug to check if it works:

 public void OnTriggerEnter (Collider other)    {
         Debug.Log("entered");
     }

This script is placed on a still object, that has a collider where is trigger is set as true.

my second object moves forward the still - this object has both ridgid body and a collider.

Have been looking around but really cannot figure out why it doesn't work when I run it (game is in 2d don't know if that changes anything)

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

2 Replies

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

Answer by tanoshimi · May 17, 2015 at 06:28 PM

If your game is 2D, you need to be using Rigidbody2D, Collider2D, and OnTriggerEnter2D. Unity's 2D physics and 3D physics sysyems are completely separate.

https://unity3d.com/learn/tutorials/modules/beginner/2d/rigidbody2d

Comment
Add comment · Show 6 · 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 Faniha · May 17, 2015 at 06:49 PM 0
Share

I had 2d colliders and rigidbodies to begin with - sorry it wasn't stated. Tried using OnTriggerEnter2D in the code, but i still don't see my debug. The video mentioned collision detection, so tried to set that as continuous, but still the same result

avatar image tanoshimi · May 17, 2015 at 06:55 PM 0
Share

Please show the actual code and a screenshot of the components attached to the objects you're using now.

avatar image Faniha · May 17, 2015 at 07:10 PM 0
Share

ok first picture alt text

code looks like this and is on the cookie gameobject:

 public class PlayerHealth : $$anonymous$$onoBehaviour {
 
     public int curHealth = 3;
 
     private SpriteRenderer myRenderer;
 
     public Sprite[] CookieLife;
 
     void Start (){
         myRenderer = GetComponent<SpriteRenderer>();
     }
 
     public void ChangeSprite()    {
         switch(curHealth){
         case 6:
             myRenderer.sprite = CookieLife[5];
             break;
         case 5:
             myRenderer.sprite = CookieLife[4];
             break;
         case 4:
             myRenderer.sprite = CookieLife[3];
             break;
         case 3:
             myRenderer.sprite = CookieLife[2];
             break;
         case 2:
             myRenderer.sprite = CookieLife[1];
             break;
         case 1:
             myRenderer.sprite = CookieLife[0];
             break;
         case 0:
             Destroy( this.gameObject );
             // end game
             break;
         }
     }
 
     public void OnTriggerEnter2d (Collider other)    {
         Debug.Log("entered");
         print ("ola");
         //if (other.tag == "Bug") {
         //    curHealth -= 1;
         //    ChangeSprite ();
         //}
     }
 
 }
 
ontriggerenter.png (145.4 kB)
avatar image Faniha · May 17, 2015 at 07:12 PM 0
Share

and thanks for helping

avatar image tanoshimi · May 17, 2015 at 07:20 PM 0
Share

You've got the name and method signature wrong. Ins$$anonymous$$d of:

 public void OnTriggerEnter2d (Collider other)

it should be:

 void OnTriggerEnter2D(Collider2D other)

(note the capital "D" in "Enter2D" and also the Collider2D parameter). http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html

Show more comments
avatar image
0

Answer by abdoubelbala · May 17, 2015 at 08:32 PM

tanoshimi has right , you have to use rigidbody2d and boxcollidder2d and you have to change the script it will be like this

  void OnTriggerEnter2D(Collider2D other) {
         Debug.Log("entered");
     }


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

20 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

Related Questions

Player ignores tilemap collider on fall 1 Answer

Distribute terrain in zones 3 Answers

OnMouseDrag issue, Event System 0 Answers

how to setting highscore with second in unity 2d? (C#) 1 Answer

Problem with disabled collision in 2D objects 0 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