Execution order AND Bundles NOT work

Why! Why this not work! How fix this?

Unity version 2021.3.11f1
I have custom order for scripts in my IK characters and rag doll controller with animations. Where i need special order to get correct result. And this work then this inside the build.

But my characters loads from bundles. And order of executing not work by default project settings.
In build index of script order 7000. But when it is loaded from bundle, the scripts get first empty position after first script 507, 508, 509…

How could such irresponsibility be allowed to the developers!
How this can be fix, without creating noodles code?

Well, generally when you have to rely on a specific execution order (with rare exceptions) it is more often than not an indicator of “noodles code”. Well, it’s actually called “spaghetti code” and in this case, i’d call it a “code smell”. Anyway …

For an example where SEO makes sense, you’d find this only in a “system” like a NetworkManager which may need to be executing before or after all other scripts relying on the network code.

Hence you’ll find only the crucial systems hooking into SEO:

I put my ClientNameTag in there simply as a quick hack for a prototype - I needed it to run after something, which now isn’t even in that project anymore but the script execution order remains because the script is in a package - ouch! This is a potential issue to happen moving forward and easy to overlook, in fact, I wouldn’t have noticed if you hadn’t given me a reason to go into the SEO settings. :wink:

SEO should only be used for global event systems, such as NetworkManager, EventSystem, InputSystem which are required to run before (or after) all other scripts by default. For instance, network messages and input state should have been fully received, processed and made available to other scripts within a given frame without knowing which scripts might rely on that information.

You do not have such a system, otherwise you’d already have a global system in SEO that runs before and/or after any assetbundle character script as needed. Therefore I’d say you’ve been following bad practice and now it bites you.

Look at it like this: you’d want to eventually refactor your scripts anyway to sequence the execution in the desired order while at the same time improving code quality. The SEO settings breaking with asset bundles is simply the first occurance where this bad practice rears its ugly head, now you have to bite that apple and fix it and, hopefully, after the refactoring you’ll come to appreciate the improvements in your code.

Within a single prefab you should totally take control by manually sequencing the code execution within the prefab rather than relying on SEO. Typically a manager script at the root level orchestrates the execution of all other scripts attached to the object deeper down in the hierarchy.

If it’s important that the sequencing occurs in relation to other prefab instances, then perhaps you’d need to have a global event system as well which you may have to put into SEO, perhaps one with a command queue like the network or input system, so that each script relying on it has a list of things to do and/or the current state at the right time (ie during Update).

I have 3 scripts in project in execution order. For update first Update in first script need create spaghetti code and full body manager for custom order execution for scripts inside the bundles. This logic best! Now i create base manager class for correct outputs debug log for 3 rows of code in my project! I need create game engage with self logic inside game engage! best!

Can u refactor this code… inside build result 1,2,3 and from bundle load result 3,1,2 || 2,1,3 ||… sometimes 1,2,3 but outside project settings order.

public class script_one: MonoBehaviour {

    private void Update() {

        Debug.Log(1);
    }
}

public class script_two: MonoBehaviour {

    private void Update() {

        Debug.Log(2);
    }
}

public class script_three: MonoBehaviour {

    private void Update() {

        Debug.Log(3);
    }
}

Control execution order yourself if you need that kind of control.

Steps to success:

  • make a MasterUpdater script MonoBehaviour
  • give it public fields for each script_one, script_two, script_three
  • name their Update methods instead public MyUpdate()
  • call them from MasterUpdater:
// in MasterUpdater.cs:
public script_one one;
public script_two two;
public script_three three;

void Update()
{
  one.MyUpdate();
  two.MyUpdate();
  three.MyUpdate();
}
1 Like

Yes, it’s really nothing special. Get the references of the scripts and call methods on them in a specific order.

If these scripts in itself don’t do much it might even be better to move that code into the main managing script of the prefab.

i create self manager of execution order in game engine what have api for execution order to update my scripts correct for fix bug of the unity developers.