Why is it that when I change scenes, Dontdestroyonload doesn’t work?

Asked

Viewed 118 times

1

I have a gameObject with the script with all the information about the level, Xp and etc for the game. It is loaded into the scene Loader, when I switch to the next scene, I don’t want to miss this gameObject.

When I use the DontDestroyOnLoad, instead of keeping the gameObject and load it into the other scenes, the name of the scene Loader changes pro in the name of gameObject. And the gameObject in the other scenes.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerStatsController : MonoBehaviour {

    public static PlayerStatsController instance;

    public int xpMultly = 1;
    public float xpFirstLevel = 100;
    public float difficultFactor = 1.5f;

    private float xpNextLevel;

    // Use this for initialization
    void Start () {
        instance = this;
        DontDestroyOnLoad(gameObject);
        SceneManager.LoadScene("GamePlay");

    }
}
  • This is very likely because of the delay that Unity uses between native and Managed, calls 'Dontdestroyonload' in Awake and the rest in 'start'.

  • Try using Dontdestoyonload in the Awake function, but still, where you have this script attached?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.