Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

DontDestroyOnLoad Getting duplicated

Discussion in '5.6 Beta' started by markpollard1, Dec 19, 2016.

  1. markpollard1

    markpollard1

    Joined:
    Apr 1, 2010
    Posts:
    70
    I have got a Gameobject with a script as shows on it.

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

    public class GameDataCarrier:MonoBehaviour{

    publicGameDatagameData;
    voidAwake(){
    DontDestroyOnLoad(gameObject);
    }
    }

    When i load the game scene the Gameobject is fine it dose not get destroyed, but when i go back to the main menu level this gameObject gets duplicated and now there is 2 and each time it makes 1 more.

    It didn't do this before this version went out.

    Can you help ?
     
  2. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    Hi markpollard1,
    Looks like you are trying to create a singleton. Here's a tutorial on how to do that in Unity:

    https://unity3d.com/de/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager

    What is strange however, is your report, that it worked the way it was intended in a different version of Unity without enforcing the singleton pattern. Could you please file a bug report with the project attached to it and reply in here with the case #?