I want to change scenes but i dont want character’s variables to reset, so I attached a script to the First Person Controller that says
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
When I get to the other scene, I get this error: " The object of type GameObject has been destroyed but you are still trying to access it."
I’ve tried using the DontDestroyOnLoad code on a cube and it worked, but it wont work for the character controller, im so confused.
I’ve searched for hours to find a solution but I couldn’t find one. How do I fix this?
I could put all the scripts i want to concerve in a cube and not destroy that cube, but I really want to know why this is happening, and how I can fix it. Also, not destroying the character controller would be ideal because if I’m not mistaken it’s position is also conserved and in my game, the player should be allowed to enter several scenes through doors, then re-enter the original scene and appear at that same door’s position.
Think i figured it out. I think everytime you load a scene your player automatically saves in the scene hierchy, and when you change scene you will get more than one off the same player in your scenes, so i found this in the referance, Hope this is it. I think we have the same problem.
Description
The object will not be saved to the scene. It will not be destroyed when a new scene is loaded.
It is your responsibility to cleanup the object manually using DestroyImmediate, otherwise it will leak.
// Instantiates a Plane everytime the game starts and never destroys it
// even if you stop your game preview
// NOTE: Watch out, this can cause LEAKS
function Start() {
var notDestructable : GameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
notDestructable.hideFlags = HideFlags.DontSave;
}
So together with the dont.destroyonload, you always only have 1 off the same player.
Didn’t work for me, I got a different error this time though, I guess I’ll have to store variables in a cube or something, thanks for help anyways though.