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
2
Question by RoR · Nov 10, 2010 at 10:28 AM · programmingfunctionoverridevirtual functions

Question about Overriding Functions

I have a base class then a subclass.

IF the base class has a OnTriggerEnter() function, how do I override that function with the subclass?

I thought, you have a base class, which you can declare it to be a virtual function, then down the line, you use override function.

THanks in advance.

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
5
Best Answer

Answer by Mike 3 · Nov 10, 2010 at 11:17 AM

In c# you do it like you say:

public class Base : MonoBehaviour { protected virtual void OnTriggerEnter(Collider other) {} }

public class Child : Base { protected override void OnTriggerEnter(Collider other) {} }

In javascript you'd just use function for both:

class Base extends MonoBehaviour { function OnTriggerEnter(other : Collider) {} }

class Child extends Base { function OnTriggerEnter(other : Collider) {} }

or in unity 2.6's javascript:

class Base extends MonoBehaviour { virtual function OnTriggerEnter(other : Collider) {} }

class Child extends Base { virtual function OnTriggerEnter(other : Collider) {} }

Comment
Add comment · Show 2 · 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 StephanK · Nov 10, 2010 at 12:02 PM 0
Share

Only that you can't have non-public virtual functions. But if you declare your OnTriggerEnter function public it will work.

avatar image Mike 3 · Nov 10, 2010 at 12:39 PM 0
Share

True, that was a thinko in my c# code. Protected is probably a better choice for the event functions though to stop you calling them manually

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

No one has followed this question yet.

Related Questions

Multiple Cars not working 1 Answer

Weird behaviour of an editor extension that sets variables on other scripts. 1 Answer

Bool function not running when called from other script 2 Answers

Get the Caller of a static function? 1 Answer

Can't Override Virtual Method In Inherited Class 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