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
-1
Question by Rphysx · Mar 31, 2014 at 05:14 PM · c#monodevelopoverride

OnTriggerEnter2D(collidertype coll) C#

Hi, I'm very new to the unity engine and I'd like to ask the community a simple question that bugs me a bit. I've studied c# with console programs and then moved to forms, and i'm generally used to create the "engine" of my applications, this is why I find it hard to understand for example how this method works.

What is exactly that calls this method and why? A particular delegate/event ? Is there some documentation that explain exactly how those kinds of methods works/gets called by the unity engine ?

Most important : If that is an inherited method from monobehaviour, why we're not supposed to override it ?

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

1 Reply

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

Answer by Hoeloe · Mar 31, 2014 at 10:31 PM

It's a method that is called when a collider with the "trigger" tag set collides with the collider attached to the same object as the script (assuming at least one of the colliding objects has a Rigidbody2D attached). It is only called once, when the colliders touch for the first time, and will not be called again between those two colliders until they separate. The purpose of this is if you want a one-time action to occur when the colliders first touch.

The reason you don't have to override it is because MonoBehaviour is not really a C# class. It's actually a C# hook into C++, and it's the C++ code that eventually calls the method. Unity's base engine is written in C++ (for speed), and the scripts you write are attached onto that engine, meaning that some of the language constraints you find in C# aren't retained between the language. It's a little messy, like a lot of Unity, and could be done in a neater and more consistent way, but that's the explanation, since you asked for it.

Comment
Add comment · Show 4 · 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 Rphysx · Apr 01, 2014 at 01:41 AM 0
Share

Thanks a lot, things are way clearer now.. Well it's a bit frustrating that I cannot completely understand why I'm doing something but I got to do it anyway since "that's how it works"

avatar image Hoeloe · Apr 01, 2014 at 09:48 AM 0
Share

What don't you understand? It's perfectly simple. Unity is running the base engine in compiled C++ (which is really just whatever machine code your OS uses) underneath your script. It hooks into your scripts when it needs to, but because it's not bound by C#s rules of inheritance, it just jumps straight to whatever it needs. This is why you don't need override, and why it can access the methods even though they're private. Yes, it's a little messy, but how it works is perfectly understandable.

avatar image Rphysx · Apr 01, 2014 at 06:14 PM 0
Share

Thanks again, I believe things will be even more clear as soon as i'll jump into c++

avatar image Hoeloe · Apr 02, 2014 at 08:42 AM 0
Share

You don't need to know anything about C++ to understand this. The thing is, most of C#'s protection systems, like access modifiers and overrides, are dealt with by the compiler - they disappear once the code is compiled. If code has been written to access it externally, interfacing with the compiled code, not the source, then you can bypass the protection schemes. This is what has been done. The fact that it happens to be done in C++ is almost irrelevant, the only reason it's not entirely is that C++ allows you to use arbitrary pointers, which makes this possible.

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

22 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 avatar image avatar image

Related Questions

Scale fit? 1 Answer

How do i use monodevelop 3.0.6 with unity 0 Answers

Issues using the 'new' keyword 3 Answers

My die function gets called without me asking it. 1 Answer

How can i shuffle a list 5 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