- Home /
Script Execution Order doesn't affect derived classes.
So I need script Start() methods to be called in a certain order. I was hoping Unity would support custom LateStart (and EarlyStart) methods or something where I can specify (in code) a priority or order number to indicate when that method is called. Unfortunately, the closest thing I could get was using the Script Execution Order menu in the editor to order when scripts' Awake()s and Start()s are called. This would be acceptable for me if setting a base class order would maintain that order for all of it's derived classes. It doesn't seem to do that. Because of that, I'd have to manually add each new derived class script I make to the order menu through the editor. If I could do this in code instead, it wouldn't be an issue, but I have no idea how to do that. This is quite horrible for work flow when the game has a lot of content in the form of derived classes.
Before I make a static class with a list of delegates to call in their specified orders on the next update, I was wondering if there was a more elegant solution.
I'm quite surprised if I have to do this manually, honestly. I don't know how people would make games where initialization orders of objects don't matter. Hopefully I'm just missing something or perhaps I'm doing something wrong entirely. Do other developers never have initialization logic that relies on other scripts' initializations? I can't even imagine how I'd code without that.. By default there's just Awake() and Start(); that only allows for 2 layers of dependencies.. In other words, if B.Start() requires A.Awake(), no problem. But if there's a C.Start() that requires B.Start(), what do you do? Especially when C has dozens, perhaps hundreds, of derived classes.
So much of my program$$anonymous$$g experience has been within Unity, I've grown accustomed to shortco$$anonymous$$gs like this.
I don't usually use Start() or Awake() at all, except in singleton-ish cases and cases where such dependencies just don't matter. I almost always create my own Initialize() method and call it when I instantiate a new instance.
Frankly I find taking such explicit control of dependencies superior to attempting to offload that responsibility to Unity's approach - for this among other reasons.
Sorry I don't have better news. ;)
Yeah but then your init() methods aren't called if objects are placed in the gameworld through the inspector and not code. What do you do about that?
Believe it or not, this rarely comes up for me. It may be that setting execution order per derivation is as good as it gets in your specific case. It always rubs me the wrong way when I seem to have no alternative.
I may be overstepping, but I believe SEO is not meant to be relied on as heavily as you describe. Call me crazy, but I only enforce execution order when there is no clean alternative whatsoever. I felt your pain early in my Unity days, having learned to program with defacto control over execution order.
As cruel and unusual as it sounds, it's worth considering developing practices which don't necessitate strict sequential dependencies. So much in Unity revolves around modularity and abstraction - you either go with their preferred principles or you struggle against them.
I'm drawing a blank on clever alternatives. The scripted enforcement you mentioned is all I'd suggest, though it sounds like a major pain. $$anonymous$$aybe it could be automated by employing a pattern which requires scripts enqueue themselves into a singleton at start time, then get sorted and initialized in an order specified by a flexible template.
I'm curious to hear alternatives too, good question!
Yeah I've learned Unity wants you to do things it's way or no way, so trying to rebel against it causes nothing but problems. Naturally this kind of pisses me off, I'd rather have the freedom and less restrictions, but that's how Unity works, so I've come to accept being Unity's bitch if it means I actually get shit done.
Problem is, I don't know how Unity wants me to handle a situation like this. The singleton alternative you described is exactly what I've been considering if there's no other way. I just didn't expect Unity to not have a built-in solution to this problem. And if it really doesn't, then I'd presume that means I'm doing something that isn't the "Unity Way", but what IS the Unity Way to do what I'm trying to do?..
For now, I shall singleton.
Answer by AlwaysSunny · Aug 24, 2014 at 10:35 PM
I wind up using a lot of "manager" singletons when statics are overly cumbersome. I usually build a chain of events starting with a GameStateManager which handles the application state, user accounts, global options, etc. This calls Init(); on a single tier of manager scripts like pool managers, audio players, visual effect services...
After that I rarely have any logic requiring sequential dependencies. It becomes quite nice once you're used to the routine.
I assume you're having this problem because you're setting things up in the scene and they all try to activate in an uncontrolled order. Best bet might be moving their activation logic into a method you call from a manager. Manager activates them in the sequence you choose based on some criteria like type or some kind of tagging system you create. Bummer, yes. Maybe SEO really IS the way to go in your case.
Your answer
Follow this Question
Related Questions
How to do combos? 1 Answer
If there is only 1 instance of a class, should it be Static? - C# 2 Answers
Unity script system 2 Answers
Print function not work with Class Constructor 1 Answer
Is every Script I drag on a Gameobject an instance? 1 Answer