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 EvilSam · Mar 04, 2014 at 09:57 AM · c#mono-behaviourconstruction

Constructors for Only-Realtime-Created Monobehaviours

What are the risks of using constructors for MonoBehaviours that will NOT be serialized/deserialized, as they are created and destroyed in run-time.

I know i can use awake, but for some reason I don't want to, please provide answers about the question itself, not suggestions for alternative approaches.

Thanks.

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

Answer by whydoidoit · Mar 04, 2014 at 10:01 AM

I can't think of any risks under those circumstances. But given you are going to have to create a parameterless constructor (because you can't construct any MonoBehaviour directly, only through AddComponent) it won't be very different from Awake.

Comment
Add comment · Show 7 · 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 EvilSam · Mar 04, 2014 at 10:10 AM 0
Share

sure it's not different, but i'm using inheritance, if the base class had a constructor, it's guaranteed that it's gonna get called, but if it had protected virtual Awake(), it might be overriden without calling base.Awake(), or calling base.Awake() later, that's why i prefer having a constructor, i don't want to give the derived class control over when and if the initialization method is called.

avatar image whydoidoit · Mar 04, 2014 at 10:11 AM 0
Share

Agreed, those are the only circumstances I can see it being useful under.

avatar image whydoidoit · Mar 04, 2014 at 10:12 AM 0
Share

BTW - the way Unity works Awake never needs to be virtual because it's reflection method calls top down. As long as the base classes have protected, internal or public Awake functions you can call them. I ended up doing it this way, but the limitations are as you say.

avatar image EvilSam · Mar 04, 2014 at 10:17 AM 0
Share

you mean that if i had non-virtual private awake in the base and the derived classes, both are gonna get called, and the base first? If so, it's new to me, and then I'm gonna go with "Awake" solution

avatar image whydoidoit · Mar 04, 2014 at 10:19 AM 0
Share

Sadly not - I mean simply you can avoid all of the

   protected virtual void Awake() {
   }

and

  protected override void Awake() {
  }

in favour of

   protected void Awake() {
       base.Awake();
   }

Technically makes the class smaller and reduces typing, but doesn't help with the enforced downwards calls.

Show more comments

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

constructing buildings rts style. 1 Answer

Displaying buildable area as an overlay on terrain 0 Answers

Flip over an object (smooth transition) 3 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