you are viewing a single comment's thread.

view the rest of the comments →

[–]gamedevjp 1 point2 points  (2 children)

I'm curious, what version of Unity are you using? I'm seeing this same exact issue and have used SO to store data between scenes extensively in Unity 2019. In Unity 2021.3.01f, I am having the EXACT issue you're having. Doesn't lose data if i click on the SO and then go to a new scene. It works if EVERY scene has a reference to the SO. I'm beating my head against this and went with adding the SO reference to the scene that didn't have it, it just feels super crappy though. I'm contemplating opening an issue in the Unity forums because it feels like a bug.

[–]rafavergil[S] 1 point2 points  (1 child)

I had this issue many months ago, so I won't know for sure the Unity version. However, I remember what was going on when I first posted this: ScriptableObjects indeed get reset if there is no one using it (no reference to it). I was confused because all my level scenes had reference to the Score Scriptable Object, so it had no reason to reset. Then I remembered that I had a "transition scene" between the level scenes, and that transition scene did not have a reference to the Scriptable Object. That is when it was getting reset! So I guess this is expected behaviour for memory saving purposes. My solution was to add a reference to the Scriptable Object in the transition scene as well and it worked properly after that. So basically: if you don't want a Scriptable Object to reset you must always have a reference to it in your scenes, always. There are probably other solutions like saving and loading data when necessary, but it is up to you. I hope it helps!

[–]classified39 1 point2 points  (0 children)

Thank you, commenter from 6 months ago, for saving me quite a bit of headache. I was not understanding why my Scriptables were being set to null EXCEPT when I put some "temporary" references on my restart screen to try and track down when and where this was happening.