Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: SawyerK on April 14, 2025, 12:36:11 PM
-
Hi,
I set global gameobjects at the beginning of a scene to use them to display text, reference player character etc... but it still causing NullReferenceException errors. It's always a different item that uses these global gameobjects that throws the error.
My guess is that the items that uses these global gameobjects call the action before the "set global gameobject" can set the global gameobject value, therefore it thinks that it's empty. Even tho both are on the very first State.
Is there a way to make sure the global gameobject is set, before anything else can reference them? My only solution that I can think of right now is to add a State to every FSM with a "Next Frame Event" but I really don't want that cause it just clogs up my FSM's.
Is there any other way?
Thank you in advance.
(https://i.postimg.cc/N04Q6DcR/globalgoerror.jpg)
-
If you have an FSM that must run before other FSMs then you have to enforce this explicitly. In other words, other FSMs have to wait until the first FSM has run. Even if it seems to work sometimes, Unity does not guarantee the update order of MonoBehaviours.
There are many ways to do this:
- Have the first FSM run then load a scene or prefab containing the other FSMs.
- Have the first FSM broadcast a "Ready" event and other FSMs wait for this event.
- Other FSMs start disabled (or in a disabled branch of the scene hierarchy) and the first FSM enables the other FSMs (again using scene hierarchy makes this easier).
- Next Frame Event will also work, but like you said it does add some complexity to other FSMs.
Those are the most common strategies. You could also try giving your globals default values so they are never null, but you have to be careful other FSM don't use these values in unintended ways.
-
Thank you for the answer.
I probably have to go with the next frame event method, due to how complex the game is.
I tried to add my globals default values but it doesn't let me for gameobject, that's why I need to set it in an FSM action.
-
This is a very good explanation of how to manage the running order of FSMs in Unity. It clearly explains the methods to ensure one FSM runs before the others and provides various strategies to handle the problem.