I have [SerializeField] linking all my UI objects to my lobby. However, when returning back to the lobby, everything’s gone! It seems that [SerializeField] only works ONE time?
Is there a way to re-serialize everything upon a scene change? It would seem impractical to GameObject.Find() everything, wouldn’t it? I feel like there has to be a way that I don’t know about
I did, but perhaps I should revisit… the only time I used this with [SerializeField] as for UNET and HLAPI – when it’d toss me back into the lobby, anything serialized was null. I just figured this is the normal thing. However, UNET is sorta abandoned and super buggy, so let me try it now in a normal scene/gameObj
Can you provide more information?
How to do get back to the lobby? Do you use DontDestroyOnLoad and perhaps destroy the objects in antoher scene?
How does one have to imagine “linking all my UI objects to my lobby.[…]when returning back to the lobby,”? What exactly are the steps that are involved?
I’d recommend to avoid the pattern with the statics and DontDestroyOnLoad. It’s not necessary if everything is set up correctly.
Data in a SerializedField are only loaded in the moment the object is created. when the lobby was created it was in the same scene as all the UI so the instances were loaded in together. your lobby (and not the UI) was then pushed to an additive scene due to having the dontdestroyonload. the references are still there at this point because the attachments were already made. as soon as you leave the scene however the UI objects are destroyed, which breaks the references in the lobby.
Going back to the scene won’t solve it because the Lobby is already created (and you’ve likely made it a singleton thus destroying the newer object that WOULD have those references). Unity Serialization happens when the object is created. but the Lobby already exists so its not getting another deserialization call.
possible solutions:
group the UI and the lobby into one object that doesn’t destroyonload
don’t have the lobby persist across scenes with dontdestroyonload
have your UI register themselves to a ScriptableObject asset(or assets) and the lobby reference via the scriptableObject
for the 3rd solution the UI will register OnEnable and unregister OnDisable. Then the Lobby simply asks that ScriptableObject for the UI it currently has (it doesn’t reference the UI directly). If the scriptableObject doesn’t have any then that means no UI exist atm or are not availible for use and the Lobby doesn’t do anything (returns). This way when the scene unloads the UI will deregister themselves and when it reloads the new instances will add themselves for the lobby to use. This Works because ScriptableObject assets persist across scenes so the Lobby won’t lose a reference to it and it also provides an easy hook for your UI to hook to (doesn’t need to find the Lobby)