PlayMaker Help & Tips > PlayMaker Help

Global GameObject Variable going Null after Scene change

(1/2) > >>

MikeTelly:
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?

jeanfabre:
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:

--- Quote from: jeanfabre 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

--- End quote ---

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.

MikeTelly:

--- Quote from: jeanfabre 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

--- End quote ---

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:
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

Navigation

[0] Message Index

[#] Next page

Go to full version