playMaker

Author Topic: [Global Variables] Losing global gameobject variable reference  (Read 546 times)

xalbertus

  • Playmaker Newbie
  • *
  • Posts: 6
[Global Variables] Losing global gameobject variable reference
« on: November 30, 2021, 06:26:27 AM »
Hi,

We use global variables to store the reference to the Player's Gameobject. The player is a persistant object, it does not destroy when changing scenes, and we set the global variable from its own FSM at the beginning.

Everything works just fine but then recently we have found out that the reference to the player's gameobject can be lost when going from scene A to scene B and the back to scene A.
It won't always happen, but it happens and we don't know why, because as I said, the Player is never destroyed, the SetGameObject action is placed just at the start of the FSM and we checked that it's not being called anywhere else.
We saw through the search that it could be a race condition but we really just set the variable there.

Any suggestions on what could be going on here?

Thanks in advance.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6865
    • jinxtergames
Re: [Global Variables] Losing global gameobject variable reference
« Reply #1 on: November 30, 2021, 09:48:44 AM »
Hi.
So far i have not seen this issue yet, what you can try is set a fsm on the player and use a Game Object Is Null and when null do a Debug Log and set it to error (this will pause the game if Error  Pause is enabled on the console.
This way you can narrow down on when it happens (just to rule out any issue in the fsms that it gets accidentally removed)


if this triggers and you still can't find you could use it as a fail save to place it back to the global (so if null, place back player to global)

So far it only happened when going from scene B to A?

xalbertus

  • Playmaker Newbie
  • *
  • Posts: 6
Re: [Global Variables] Losing global gameobject variable reference
« Reply #2 on: November 30, 2021, 10:05:38 AM »
Hi.
So far i have not seen this issue yet, what you can try is set a fsm on the player and use a Game Object Is Null and when null do a Debug Log and set it to error (this will pause the game if Error  Pause is enabled on the console.
This way you can narrow down on when it happens (just to rule out any issue in the fsms that it gets accidentally removed)


if this triggers and you still can't find you could use it as a fail save to place it back to the global (so if null, place back player to global)

So far it only happened when going from scene B to A?

Hey, thanks for the response.

The issue looks like the one in this post:
https://hutonggames.com/playmakerforum/index.php?topic=13276.0

It's the same result but it's not the same problem in our opinion because we don't have the player or the gamemanager in our scenes, we instantiate them at first and don't destroy them when changing scenes. Our game flow is:
1.- We create Player that sets the global variable Player at the Start state.
2.- We go to another scene. The player remains with DontDestroyOnLoad and Start state is not being called
3.- The global variable is still there
4.- We go back to the previous scene. The player remains with DontDestroyOnLoad and Start state is not being called
5.- When the old scene is unloaded the global variable is now set to None (or Missing).

The global reference is lost whenever we go from one scene to another and then back to the first one, no matter which scene is A and which scene is B. It occurs with all of our game scenes.

We know exactly when the reference is lost, we just don't know why. Is it possible that the global variables are reset after a while or something like that?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6865
    • jinxtergames
Re: [Global Variables] Losing global gameobject variable reference
« Reply #3 on: November 30, 2021, 10:11:23 AM »
Hi.
Have you also set a 'Singleton Manager' Reference (Ecosystem)

on the object that you are not destroying

xalbertus

  • Playmaker Newbie
  • *
  • Posts: 6
Re: [Global Variables] Losing global gameobject variable reference
« Reply #4 on: November 30, 2021, 10:17:43 AM »
Hi.
Have you also set a 'Singleton Manager' Reference (Ecosystem)

on the object that you are not destroying

We are not using Playmaker to create the Singletons, we use our own scripts. Is that what you mean?

BTW our player is not / doesn't have a Singleton.
« Last Edit: November 30, 2021, 10:46:04 AM by xalbertus »

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1284
    • Fat Pug Studio
Re: [Global Variables] Losing global gameobject variable reference
« Reply #5 on: November 30, 2021, 02:52:41 PM »
It's a bug, i believe i reported it a long time ago. But i'm having it even worse, the global variable is lost during gameplay (for example, MainCamera) i'm not even switching scenes.

I'm usually forced to use some globals since it would be gruesome to use locals for everything, i use an FSM on all global game objects that have get owner and loops into itself every second  ::)
« Last Edit: December 01, 2021, 03:51:12 AM by Fat Pug Studio »
Available for Playmaker work

xalbertus

  • Playmaker Newbie
  • *
  • Posts: 6
Re: [Global Variables] Losing global gameobject variable reference
« Reply #6 on: December 01, 2021, 07:06:37 AM »
It's a bug, i believe i reported it a long time ago. But i'm having it even worse, the global variable is lost during gameplay (for example, MainCamera) i'm not even switching scenes.

I'm usually forced to use some globals since it would be gruesome to use locals for everything, i use an FSM on all global game objects that have get owner and loops into itself every second  ::)

That's a thing we prefer not to do, at least not for now, but it's being really annoying.

@djaydino can you answer what did you mean with the Singleton Manager and if that's a thing we need even if the player is not a singleton?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6865
    • jinxtergames
Re: [Global Variables] Losing global gameobject variable reference
« Reply #7 on: December 01, 2021, 09:34:02 AM »
Hi.
The singleton will make sure that the reference will have only 1 instance.

for example scene 1 has object A and this object has a 'Don't Destroy On Load'

when going to scene 2 object A stays alive.

When going back to scene 1 object A is there and can make a 2nd don't destroy.

with a singleton manager @ the start state, the object will be deleted if it already exists before any code starts.

You can try adding it, it won't break anything.
only if you want to destroy the object, for example when going to main menu,
then use "Delete Singleton Reference"

xalbertus

  • Playmaker Newbie
  • *
  • Posts: 6
Re: [Global Variables] Losing global gameobject variable reference
« Reply #8 on: December 01, 2021, 09:37:17 AM »
Hi.
The singleton will make sure that the reference will have only 1 instance.

for example scene 1 has object A and this object has a 'Don't Destroy On Load'

when going to scene 2 object A stays alive.

When going back to scene 1 object A is there and can make a 2nd don't destroy.

with a singleton manager @ the start state, the object will be deleted if it already exists before any code starts.

You can try adding it, it won't break anything.
only if you want to destroy the object, for example when going to main menu,
then use "Delete Singleton Reference"
Hmmm the thing is, we don't really have the Player in the scene, we Instantiate it at the start of the game, and it remains the same gameobject until you hit Stop. Should we try this anyway?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6865
    • jinxtergames
Re: [Global Variables] Losing global gameobject variable reference
« Reply #9 on: December 01, 2021, 09:43:12 AM »
Hi.
It won't hurt to try :)
it will not hit any performance or so.

xalbertus

  • Playmaker Newbie
  • *
  • Posts: 6
Re: [Global Variables] Losing global gameobject variable reference
« Reply #10 on: December 01, 2021, 10:46:48 AM »
Hi.
It won't hurt to try :)
it will not hit any performance or so.

So you are saying to add this action:

https://github.com/PlayMakerEcosystem/PlayMakerCustomActions_U2018/blob/8b8772976f2f70563261dc407ab35e37f7e766c8/Assets/PlayMaker%20Custom%20Actions/GameObject/SingletonManager.cs

To the Player FSM?
I don't really see how that can affect the global variable missing its value...

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6865
    • jinxtergames
Re: [Global Variables] Losing global gameobject variable reference
« Reply #11 on: December 01, 2021, 10:56:55 AM »
Hi.
So far in my projects i always use singleton if i use a don't destroy.
and so far i never had the issue yet loosing connection to my globals.

not saying that it will help, but it does not hurt to try and it makes it 100% sure there are no other instances of a object.

A possibility loosing connection can also happen if you edited prefabs and you for example set a global variable, then undo.
i don't know if in latest version it is fixed, but in 1.9.0 it happens.
But it get fixed again when restarting unity.
But i don't think this is your case/issue here.