Search Unity

Switch statement

Discussion in 'Scripting' started by Axteryo, Jan 16, 2014.

  1. Axteryo

    Axteryo

    Joined:
    Jan 15, 2014
    Posts:
    7
    Hello, I wrote a switch statement and used another script to switch it's case, but the switch statement keeps defaulting back to the first case. How can i fix this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Post code.

    --Eric
     
  3. Axteryo

    Axteryo

    Joined:
    Jan 15, 2014
    Posts:
    7
    My enum plus the switch statement in the update function.

    Code (csharp):
    1.  
    2. public enum GameState
    3.     {
    4.         MainMenu,
    5.         CurLevel,
    6.         GameOver,
    7.         Score
    8.     }
    9.     public GameState gs;
    10.    
    11.    
    12.     // Update is called once per frame
    13.     void Update ()
    14.     {
    15.        
    16.         print ("The gamestate is currently on: " + gs);
    17.         switch(gs)
    18.         {
    19.         case GameState.MainMenu:{
    20.             //Debug.Log("Game is currently on the main menu");
    21.             break;
    22.         }
    23.         case GameState.CurLevel:{
    24.            
    25.             //Debug.Log("Game is currently on level: " + Application.loadedLevelName);
    26.             GamePaused();
    27.             Debug.Log ("Game pausing enabled");
    28.             GamePlaying();
    29.             //mm.enabled = !mm.enabled;
    30.             break;
    31.         }
    32.         case GameState.GameOver:{
    33.             GameRestart();
    34.             break;
    35.         }
    36.         case GameState.Score:{
    37.             break;
    38.         }
    39.            
    40.         }
    41.     }


    The code that changes the switch case supposedly.

    Code (csharp):
    1.  if (GUI.Button(new Rect(10, 70, 50, 30), "Level 1"))
    2.         {
    3.             //Application.LoadLevel(1);
    4.             Debug.Log("Clicked level 1 button");
    5.             gamemanager.instance.gs = gamemanager.GameState.CurLevel;
    6.            
    7.        
    8.         }
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I assume it has something to do with how you're referring to the manager script, but you haven't shown how that's set up so I'm not sure. By the way, enums are global (within a namespace) so you can just say GameState.CurLevel, you don't need to qualify it.

    --Eric
     
  5. Axteryo

    Axteryo

    Joined:
    Jan 15, 2014
    Posts:
    7
    Code (csharp):
    1.  
    2.    
    3.     public Texture btnTexture;
    4.     public GameObject gamOBJ;
    5.     private gamemanager gam;
    6.    
    7.     // Use this for initialization
    8.     void Awake () {
    9.        
    10.         gam = gamOBJ.GetComponent<gamemanager>();
    11.    
    12.     }
    13.  
    this is how i reference the manager script.
     
  6. Axteryo

    Axteryo

    Joined:
    Jan 15, 2014
    Posts:
    7
    I'd really appreciate it if somebody could help me.
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    A variable doesn't change value all by itself... I have the feeling your got other object accessing it and modifying somewhere else, other than the code snippets you posted.
     
  8. Axteryo

    Axteryo

    Joined:
    Jan 15, 2014
    Posts:
    7
    thank you i will take a look through all of my code, because this should not be happening. I also have a question, when a script is started up, are the variables declared before the awake function?