Hello! There is a problem with dontDestroyOnLoad function. I use it for my player, so when it goes to next level, everything is ok, but every level has scripts. Every script has variables such as “Player”, “playerCam” etc, and when i load next level in game , there is an error
You probably need to assign the Player variable of the deActivateJumping script in the inspector.
I understand that it isn’t assigned in scene, but there is no player in scene, so how it can be solved?(
So your deActivateJumping script is losing the reference to Player when you load a new level. This means you need to do one of the following:
- DontDestroyOnLoad the Player GameObject that you are (probably) linking to deActivateJumping in the Editor.
- Instantiate a new Player before deActivateJumping needs it
- stop the deActivateJumping script from running until a Player has spawned in
- Option 4, find Player in Awake by Tag. Make sure your Player has a tag that makes it easy to find. Please understand this is two steps if your Player is a script and not a GameObject. First you must find the actual Player Object, then pass the reference of the Player script.
L
private void Awake() {
GameObject playerGameObject = GameObject.FindGameObjectWithTag("Player");
Player = playerGameObject.GetComponent<Player>();
}
Unity Documentation & JS/Boo Examples: http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html
You can use singleton pattern for Player with no need to use the reference assignments.