playMaker

Author Topic: Global GameObject Variable going Null after Scene change  (Read 3167 times)

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Global GameObject Variable going Null after Scene change
« on: March 20, 2018, 10:34:16 AM »
So I'm trying to create a gameobject that stores itself as a global gameobject variable, while at the same time being able to move it from scene to scene.

I originally gave the object an FSM with two states. The first (Start) has the Singleton Manager action I found on the Ecosystem, and the second has Do Not Destroy On Load and Get Owner to set itself as a global variable.

Moving from the original scene to any other scene is fine. The global variable keeps a reference to the gameobject. The issue occurs when I return to the original scene (where the object was first instantiated). No matter what I do (changing order of states, shutting off Singleton Manager etc.), the global variable that had the gameobject goes null when that original scene loads up. I even created a separate game object that spawned in the object in question after 3 seconds to test whether it needed to be in the scene initially for the issue to occur. The variable still went null once the scene loaded up.

I understand that I can probably just load up the gameobject in some very early scene that never reloads again, but why do I need to do this? Why is this variable going null?
« Last Edit: March 20, 2018, 10:42:24 AM by MikeTelly »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Global GameObject Variable going Null after Scene change
« Reply #1 on: March 21, 2018, 12:50:37 PM »
Hi,

It does look like a glitch in your logic or setup, loading scene is tricky however because component life cycle is messy during that transition.

 If you can isolate this in a project, with just PlayMaker and two scenes with this, I'll have a look and submit a but report if I can repro and locate that it's indeed an issue with PlayMaker.

 Bye,

 Jean

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Re: Global GameObject Variable going Null after Scene change
« Reply #2 on: March 21, 2018, 11:28:45 PM »
Hi,

It does look like a glitch in your logic or setup, loading scene is tricky however because component life cycle is messy during that transition.

 If you can isolate this in a project, with just PlayMaker and two scenes with this, I'll have a look and submit a but report if I can repro and locate that it's indeed an issue with PlayMaker.

 Bye,

 Jean

So I decided to do just that (create a new project) and the variable did keep it's value when I returned to the original scene. I went back to my original project and decided to start removing other objects from the scene to locate what was causing it. There were other FSMs in the scene when I first tested this, but I deactivated them, so the only things that were activated was the Player, the floor, and the trigger object that initiates the scene change (it's the player by the way that's setting itself as a global variable).

I ended up isolating the issue to an FSM on a different object. It sets the player to the correct position in the scene using Set Position just when the scene loads up (the object that has this FSM is loaded with the scene). If I remove the state that contains Set Position (which sets the position of the Player gameobject global variable), the global variable does not go null.

There is another case of a different gameobject global variable going null. This time it's fixed when I delete a state from an FSM that contains a Send Event with the target being the object that set itself as a global variable.

Even if I deactivate the objects that cause the problem, it doesn't fix the issue. They need to flat out not be in the Hierarchy.

So basically...
- My global variables keep going null if I return to the scene where these variables were originally set.
- This occurs because other objects in the original scene reference it (this doesn't occur on the first time in the scene, or in any other loaded scene, even if the objects that cause the issue in the original scene have instances in subsequently loaded scenes). This is only a problem on a return visit to the original scene.
- It doesn't matter if the other FSMs that cause the issue are deactivated or not. They must not be in the scene for the variable to not go null.

It's... really strange.
« Last Edit: March 21, 2018, 11:35:55 PM by MikeTelly »

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Re: Global GameObject Variable going Null after Scene change
« Reply #3 on: March 23, 2018, 01:37:01 AM »
Hi,

It does look like a glitch in your logic or setup, loading scene is tricky however because component life cycle is messy during that transition.

 If you can isolate this in a project, with just PlayMaker and two scenes with this, I'll have a look and submit a but report if I can repro and locate that it's indeed an issue with PlayMaker.

 Bye,

 Jean

So I went back and did more testing and discovered something potentially very important. If I break the prefab instance of whatever is referencing the gameobject global variable, it will NOT be set to null upon reentering the scene. I'm assuming this issue has to do with how prefabs load up then.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Global GameObject Variable going Null after Scene change
« Reply #4 on: March 26, 2018, 02:13:05 AM »
Hi,
 
I have a hard time wrapping my head around what you wrote. Could you do a step by step of the issue? thanks :) I'll try to repro locally.

 Bye,

 Jean

MikeTelly

  • Playmaker Newbie
  • *
  • Posts: 40
Re: Global GameObject Variable going Null after Scene change
« Reply #5 on: March 26, 2018, 02:57:46 AM »
Hi,
 
I have a hard time wrapping my head around what you wrote. Could you do a step by step of the issue? thanks :) I'll try to repro locally.

 Bye,

 Jean

I'll try to make it more clear. Sorry about that. My other posts are a bit scatterbrained since I was continuously trying to isolate the issue and kept finding new interactions.

So basically my issue is very similar to this case (just there was no updating of Playmaker or Unity).

http://hutonggames.com/playmakerforum/index.php?topic=13910.0

If I return to the original scene, the global variables that reference the persistent gameobjects go null. What I found is that these global variables only go null if other FSMs in the original scene use those global variables in one of their states.

For example I have an FSM on a gameobject called "Teleport Manager" that teleports the player as soon as the scene loads up. The teleport is done through Set Position and uses the "player" global variable to get reference to them. Currently when the scene loads up, the player isn't teleported since the variable containing the reference to them goes null. If I delete the State that contains Set Position though, the global variable doesn't go null ever.

Furthermore, I found that this whole "global variables going null" thing only happens if the FSM that uses the global variable is a prefab. If I break the prefab instance of "Teleport Manager", the global variable doesn't go null and the player is teleported.
« Last Edit: March 26, 2018, 02:59:59 AM by MikeTelly »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Global GameObject Variable going Null after Scene change
« Reply #6 on: April 03, 2018, 03:19:03 AM »
Hi,

 uhm, this is odd. I'll investigate further on this.

 Bye,

 Jean

monsterblues

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Global GameObject Variable going Null after Scene change
« Reply #7 on: March 23, 2019, 09:52:21 PM »
I'm having the same issue in the exact same way. MikeTelly's post could have been written by me.

I have the following:

SceneA
SceneB
SpawnPointFSM
PlayerPrefab set as a global variable (Do Not Destroy On Load)

SceneA will load SceneB, then SceneB will load SceneA.
SceneA and B have a SpawnPointFSM prefab at the starting locations.
The SpawnPointFSM in the scene checks if the Player exists and moves it to its location. If Player doesn't exist it creates one at it's location.

If I play SceneA, go to B, then back to A the global variable is unset. It happens in Set Position too.

Unless I break the SpawnPointFSM prefab as MikeTelly suggested.

Man I spent a couple days on this. :(

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Global GameObject Variable going Null after Scene change
« Reply #8 on: March 29, 2019, 02:28:21 AM »
Hi,

 you can't keep gameobject references between scene loading because they are gone, unless you flag that gameobject as "don't destroy on load" in which case it will survive loading.

 in your case, I guess you have defined that gameobject in both scene A and scene B right? then, tag this object with a unique tag, and use the action FindGameobject and search by this tag ( better than by name), and then use it, this way you don't have to store it.

 if you want to store it, make an fsm on that gameobject on both scene, and the fsm is responsible to setting the global variable with itself. then everytime you load a scene, the object fsm will set the global variable for other fsm to use it.

 Bye,

 Jean