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.

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).

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().