Avoiding script order of execution issues

I’m making a basic 2D platformer. I’ve got my basic character movement done (not using the character controller), and I’ve just added a basic moving platform with its own simple script to move it up and down.
The problem is that the character’s script is clearly running first, so it’s positioning the character at the platform’s y position before the platform’s movement gets updated. The result of course is that my character lags one frame behind, and is either slightly too high or low depending on whether the platform is moving up and down.

From my searches I know that I can’t rely on the order of execution of scripts and can’t set it myself, but I’ve been unable to figure out the correct way to do it.
If it were simply a few objects in the game, I’d probably rename my Update function, and use another script to call them in the correct order, but I need a method that would work for large levels where I need things to update themselves without manually handling every individual object.

What is the correct way of ensuring an objects’ updating gets run in the order I want?

Make a single game object that initialize all other game object, in the right order, so you can ensure the order.

From your story, I gather that you are not using the conventional method of implementing a platform (i.e. parenting the player to the platform object). Is there a good reason not to? It should solve your problem.

As for the actual question: if you need to make sure the platform script Updates after the player, you could use LateUpdate instead of Update for them. LateUpdate is called after all Updates are done. Camera scripts usually use this function to make sure they always update after the player has moved (which means putting the player in LateUpdate instead of the platforms is probably a bad idea).

“If it were simply a few objects in the game, I’d probably rename my Update function, and use another script to call them in the correct order, but I need a method that would work for large levels where I need things to update themselves without manually handling every individual object.”

Where are you running into the problem? At start or during normal running? How many objects need to be delayed and how long for?

With the following features one could do a simple but inefficient update spam:

And simply put everything inside a single game object or try and find all game objects by tags:

Or Unity - Scripting API: Object.FindObjectsOfType

I’m sorry, but what do you mean by initialize the other game objects? The issue isn’t on starting up, it’s only while running. (sorry if I’m misinterpreting you)

Since the camera needs to be updated after the player, who needs to be updated after the platform, LateUpdate and Update alone can’t really guarantee ordering when dealing with more than two items in a sequence. Regardless, the moving platform problem is only an example of the kinds of problems this issue could cause, so I’m looking for the general structure for this kind of problem, rather than just this specific problem. Maybe I should just fix this issue and worry about other problems if I come to them. I’m still learning Unity, so I’m still figuring out the right and wrong way to code everything, and wasn’t sure if this was a common problem to have to deal with.

I will switch to parenting the player to the platform though. There’s no reason I’m not doing it that way other than just not being at that point yet. It’s just that up/down platforms happened to already work with my current system without modification (aside from this issue of course). Left/right moving platforms will require parenting regardless of whether I fix this problem.