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 /
This question was closed Nov 10, 2017 at 09:24 PM by Mortup for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Mortup · Nov 09, 2017 at 02:11 PM · componentcomponentsinitializationconstructors

How to make sure custom init methods are called?

I'm having a little trouble with Unity's component approach. I've created an Dad class that extends MonoBehaviour and a Family class that also extends MonoBehaviour but also has a reference to Dad.


I can make sure that Family has always a Dad reference by using [RequireComponent], the problem is that every Dad should have a parameter 'name', and this parameter must be set by it's Family.


As I shouldn't use constructors on MonoBehaviours I've created a method to set the name:

  public void Init(string aName) {
      this.name = aName;
 }



But I want to receive an error when I forgot to call Init. Is there a way to make sure the Init method will always be called?

Comment
Add comment · Show 1
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 Harinezumi · Nov 09, 2017 at 02:20 PM 0
Share

Shouldn't it actually be the other way around, that a Dad always has to have a Family? Then every Dad will always get a name.

Anyway, I think what you want to do here is make sure that Dad.name is always set when you want to use it. For that purpose you can have a getter function or property which checks that:

 public class Dad : $$anonymous$$onobehaviour {
 
     private string name;
     public string Name {
         get {
             if (string.IsNullOrEmpty(name)) {
                 Debug.LogError("Dad " + gameObject.name + " does not have a name!");
             }
             return name;
         }
     }
 
 ...
 
 }

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by xarismax · Nov 09, 2017 at 06:37 PM

At Family.Start() -> call Dad.Init(name);

Think as Dad as a "Data Object", Family is responsible for calling its methods. For this reason don't add Awake or Start on Dad, only Family class is allowed to initialise Dad.

1) Initialization Responsibility: This makes explicit that Dad is not responsible for initializing itself, the responsibility falls to another class (Family).

2) Explicit Execution Order: This ensures that Family will be called before Dad, as Dad is initialized from Family before used.

Comment
Add comment · Show 3 · 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 Mortup · Nov 09, 2017 at 06:47 PM 0
Share

Yeah, that's what I was going for, but what if when I create a different class Another$$anonymous$$indOfFamily that also uses Dad? Every time I use the Dad component in another class I would have to remember to call Init.

avatar image Harinezumi Mortup · Nov 10, 2017 at 10:06 AM 1
Share

That's what I was referring to: either you remember to call it, or you reverse the dependency. For example you could create a Family interface/abstract base class FamilyABC, and in Dad.Start() do FamilyABC family = GetComponent<FamilyABC>(); if (family != null) { family.Init(this); }.

avatar image Mortup Harinezumi · Nov 10, 2017 at 09:23 PM 0
Share

Thanks! That should work.

Follow this Question

Answers Answers and Comments

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

Related Questions

Zenject add component dynamically 2 Answers

OnEnable not called after all Awake and not all OnDisable before OnDestroy? 1 Answer

2D Animation does not start 1 Answer

.enabled not working 2 Answers

Can't disable component, little checkbox is missing from inspector 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