Zoey S
Zoey S

Reputation: 35

player prefab script execution order

Not sure what the issue is so i'll just try to describe the outcome.

I have a player object and a UI bar that corresponds to variables from the player script, everything is working as intended, i then delete my player object and replace it with an identical prefab of the player, i reassign the player object to all the fields in the inspector where it was previously attached.

now, when i hit play, the UI is acting as if the UI script is being executed after the player controller script, and some of the values are off, to test this i changed the order of execution in the 'Script Execution Order' panel, and this seem to resolve the issue, but i would prefer not to resort to doing this.

i would like to know what can cause such behavior and how to avoid it, hopefully my explanation was clear enough.

Thanks.

Upvotes: 2

Views: 219

Answers (1)

I believe that the general rule is "whatever order they were loaded in."

This means that scripts in the scene will run before ones attached to prefabs instantiated into the scene, which will run before ones loaded from asset bundles, which run before ones loaded by additional scenes (assuming, of course, that the prefabs are instantiated before the asset bundle loads, which itself happens before the scene transition).

Which makes it...unreliable, as you've found. It's essentially a single List<Component> that Unity keeps track of, adding newly created script instances to the end, and iterating over it as needed to call the relevant functions.

The only control you have, then, is the manual execution order setting (as you've found) or not relying on Unity's script handler, but doing it manually yourself with your own code.

Upvotes: 3

Related Questions