Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Singeton + enum + DontDestroyOnLoad = problem

Discussion in 'Scripting' started by Szemkiel, Dec 17, 2013.

  1. Szemkiel

    Szemkiel

    Joined:
    Aug 26, 2013
    Posts:
    2
    Hello

    I have problem with my code.

    I have the Dungeon component which creates my map. When player finds portal on this map this component loads scene once again with different map.

    Code (csharp):
    1.  
    2. public enum DungeonOrder{
    3.     NewDungeon,
    4.     GoWest,
    5.     GoNorth,
    6.     GoEast,
    7.     GoSouth
    8. };
    9.  
    10. public class Dungeon : MonoSingleton<Dungeon>
    11. {
    12.  
    13.     public DungeonOrder order = DungeonOrder.NewDungeon;
    14.  
    15.    
    16.     void Awake()
    17.     {
    18.        
    19.         if(instance == null)
    20.         {
    21.             instance = this;
    22.             DontDestroyOnLoad(this.gameObject);
    23.  
    24.         }
    25.         else
    26.             DestroyImmediate(this.gameObject);
    27.        
    28.     switch (order)
    29.         {
    30.             case DungeonOrder.NewDungeon:
    31.            
    32.                     CreateNewDungeon();
    33.                     break;
    34.            
    35.             case DungeonOrder.GoEast:
    36.                
    37.                     GoEast();
    38.                     break;
    39.            
    40.             case DungeonOrder.GoWest:
    41.                
    42.                     GoWest();
    43.                     break;
    44.            
    45.             case DungeonOrder.GoNorth:
    46.                
    47.                     GoNorth();
    48.                     break;
    49.            
    50.             case DungeonOrder.GoSouth:
    51.                
    52.                     GoSouth();
    53.                     break;
    54.            
    55.             default:
    56.                     CreateNewDungeon();
    57.                     break;
    58.         }
    59.        
    60.        
    61.     }
    62.  
    63. // this function is called by portals.. parameter is like GoEast etc.
    64.     public void GameEvent(DungeonOrder o)
    65.     {
    66.         order = o;
    67.         nextMap(); // <-- Application.LoadLevel(Application.loadedLevel);
    68.     }
    69.  
    70. }
    71.  
    72.  
    My problem: first map loads with no problems(i think;)) but when stage loads once again order variable still is equal to NewDungeon. it should be GoWest or GoNorth..etc.

    Other variables from Dungeon object don't change. I have no other reference to this variable... I have no idea...


    thank you for help
     
    Last edited: Dec 17, 2013
  2. probbins

    probbins

    Joined:
    Oct 19, 2010
    Posts:
    216
  3. Szemkiel

    Szemkiel

    Joined:
    Aug 26, 2013
    Posts:
    2
    Thank you for tip about Destroy.

    I declare order variable as static and it looks ok right now.. any idea why this variable was set to default value durning re-load of scene?
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    A guess...

    When you reload the scene, does another Dungeon object get loaded? If it is reloaded, it will destroy itself on Awake() because a singleton instance already exists, but maybe it also goes through the switch statement thus running CreateNewDungeon()? This seems a little strange because you have DestroyImmediate() running but it may finish the Awake() anyways.

    EDIT: Try only running the switch statement if (instance == null) was true, so we are keeping the instance, so we should actually go through the switch.