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. Dismiss Notice

Beginners C# Question

Discussion in 'Scripting' started by 3itg, Jan 22, 2013.

  1. 3itg

    3itg

    Joined:
    Jan 22, 2013
    Posts:
    5
    I started teaching myself C# about an hour ago...
    But after adding conditions to my splash screen, it no longer loads the other images.
    What I expected was:
    Game Loads.
    Splash Screen1 ( backgroundTexture1) is shown.
    When any button is clicked, Splash Screen2 ( backgroundTexture2) is shown.
    When any button is clicked, Game loads level 1.

    what happens is:
    Game Loads
    Splash Screen1 ( backgroundTexture1) is shown.
    When any button is clicked, Game loads level 1.

    I am not shy in admitting that I just started, so I don't know where I went wrong.
    C# seemed to be pretty logical and straightforward... until my logic stopped working :(

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MainMenu : MonoBehaviour {
    5.  
    6.     public Texture backgroundTexture1;
    7.     public Texture backgroundTexture2;
    8.     public int Continue;
    9.  
    10.     //initialization
    11.     void Start(){
    12.         Continue = 0;
    13.     }
    14.     // Update is called once per frame
    15.     void OnGUI ()
    16.     {
    17.         GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundTexture1);
    18.         if (Input.anyKeyDown  Continue == 0){
    19.             Continue = 1;
    20.         }
    21.         if (Continue == 1){
    22.             GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundTexture2);
    23.         }
    24.         if (Continue == 1  Input.anyKeyDown) {
    25.             Continue = 2;
    26.         }
    27.         if (Continue == 2){
    28.             GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundTexture2);
    29.         }
    30.         if (Input.anyKeyDown  Continue == 2){
    31.             Continue = 3;
    32.         }
    33.         if (Continue == 3){
    34.             Application.LoadLevel (1);
    35.         }
    36.     }
    37. }
    any help?
     
  2. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    not sure if this will help but use "else if" since you say if(X=0) X = 1
    and after making X=1 you ask
    if(X=1) X=2

    so
    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.      
    5.     public class MainMenu : MonoBehaviour {
    6.      
    7.         public Texture backgroundTexture1;
    8.         public Texture backgroundTexture2;
    9.         public int Continue;
    10.      
    11.         //initialization
    12.         void Start(){
    13.             Continue = 0;
    14.         }
    15.         // Update is called once per frame
    16.         void OnGUI ()
    17.         {
    18.             GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundTexture1);
    19.  
    20.             if (Input.anyKeyDown  Continue == 0){
    21.                 Continue = 1;
    22.             }else  if (Continue == 1  Input.anyKeyDown) {
    23.                 Continue = 2;
    24.             } else if (Input.anyKeyDown  Continue == 2){
    25.                 Continue = 3;
    26.             }
    27.  
    28.             if (Continue == 1){
    29.                 GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundTexture2);
    30.             }
    31.            
    32.             if (Continue == 2){
    33.                 GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundTexture2);
    34.             }
    35.            
    36.             if (Continue == 3){
    37.                 Application.LoadLevel (1);
    38.             }
    39.         }
    40.     }
    41.  
     
  3. 3itg

    3itg

    Joined:
    Jan 22, 2013
    Posts:
    5
    thank you for the extremely fast response.
    Worked perfectly.

    Am I correct when I assume the reason my code did not work as expected, but yours did, is because the way I wrote it the sequence would happen within a single click of any button?
    i.e: Game Loads.
    Shows image 1.
    anyKeyDown changes Continue to 1
    but since the key has not been released,
    anyKeyDown then changes Continue to 2 3 and loads the level, from a single key press?

    I imagine Continue had to be getting to "3" in order for me to see the level load.
    That's my guess...
    sorry for being a noob, but your answers help me ask less in the future :)

    If im not understanding how I went wrong, then please correct me.
     
    Last edited: Jan 22, 2013