Can I change the order of the components that gets disable when changing a scene?
I have an object with two scripts, script A with actual properties and script B to save them.
Script B access script A via GetComponent() when calling OnDisable .
But using OnDisable sometimes gives me a null reference from script B at the line where i am accessing script A, so I concluded that script A is destroyed first!
I see, so you want to save stuff before changing scenes. My suggestion would be not to do it inside the OnDisable callback since you can encounter issues like the one you are experiencing.
What I would to is to save the data during “Game Over”, I’m pretty sure that somewhere in your code you have a method call or an execution path that signals the end of the game (when player health reaches 0 or maybe right before calling SceneManager.LoadScene), that’s where I would put the data saving code, and not at disable since that get’s called when everything is being destroyed so you can’t be sure that the data you want will still be there.