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 sintua · Feb 24, 2013 at 05:38 AM · c#awakeconstructors

Constructor "functionality" without Constructors?

So I'm aware that you shouldn't use constructors for gameOjects... how do you then assure that a object has all the parameters it needs?

For example, I have a Person object with a name, abilities, location, biography, etc. Normally I'd have a Person constructor that takes in these things as parameters, since there shouldn't be a Person without them.

Since Constructors are "bad", when I use Instantiate, I can create a Person who doesn't have any name/location/etc. Then in whatever I use to instantiate, I have to make sure I assign to every variable required without missing one, and also make sure that the object isn't interacted with in some form before it completes.

Say, if I'm drawing people on the screen with their names above their head. What if the GUI update runs and tried to draw this person before their various generators finish populating the Person variables? I've considered making a list of "finished people" that they're added to when the generator is done, but this all seems very backwards as opposed to just using a Constructor.

If I was to basically put all this logic in Awake(), and have the Person class look at a reference to the "Factory" (with all the bulky variable generators) and use its methods to generate its data, will that fire before other updates run? Is that what I'm actually supposed to do in this case or is it as messy as it looks?

There's no reason for a Person to know the Factory, but in this case every Person has to have that reference.

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

Answer by Tarlius · Feb 24, 2013 at 07:23 AM

I think its really the order of script execution you're really after:

  • Instantiate() (in the calling script, which is then paused)

  • Awake()

  • The rest of the calling script

  • OnEnable() (if still enabled)

  • Start()

  • Update()/OnGUI()/etc

I agree that getting the new object to call objects in the factory would be a bad design decision. I personally do all my GetComponents/etc in Awake(), then use an Initialise to sort out the rest. If the object can be initialised in many ways, I'd overload Initialise.

The convention, by the way, is in Awake() you set up links and any necessary internal logic, then Start() you perform external logic, so all classes have had a chance to set themselves up (although you can also alter the order classes execute their Awake() if needed).

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

Answer by robertbu · Feb 24, 2013 at 06:04 AM

You have options to work around the problem. As you mentioned, you can assign instance variables. If you are concerned about everything initializing, you can log and error if critical variables have not be changed from their default values. Or you could create an Initialize() method with parameters that sets instance variables and an 'initialized' flag. You would call this right after Instantiate(). In Start() you can check the flag and log an error if Initialized() was not called.

Once the instance variables are set, any logic that would normally be in a constructor could be in Start().

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Update and Awake not being called. 1 Answer

Acces an Awake variable 2 Answers

initialize with object name 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