2016-01-20 121 views
1

我開始學習Unity和C#因爲我想製作一款遊戲。但由於按鈕問題,我被卡在這裏。按鈕需要點擊兩次才能隱藏

當我運行下面的代碼與Unity時,我得到的標籤和「跳過」按鈕,因爲我想。

但是當我按下按鈕時,只有切換出來。標籤和按鈕不隱藏。那麼只有當我第二次點擊它時,它們纔會隱藏起來。

我該如何解決這個問題,以便通過單擊按鈕「跳過」隱藏按鈕和標籤並顯示切換。

感謝您查看我的問題。

void OnGUI() 
{ 
    if (!textfin) 
    { 
     GUI.Label(new Rect(0, 0, Screen.width, Screen.height), text, guiStyle); 
     if (GUI.Button(new Rect(Screen.width * 4/5, 0, Screen.width/5, Screen.height/5), "SKIP")) 
     { 
      textfin = true; 
     } 
    } 
    else{ 
     easy = GUI.Toggle(new Rect(Screen.width/4, 0, Screen.width/2, Screen.height * 3/8), easy, "easy"); 
     if (easy) 
     { 
      normal = false; 
      hard = false; 
      Title.difficulty = 1; 
     } 
     normal = GUI.Toggle(new Rect(Screen.width/4, Screen.height * 5/16, Screen.width/2, Screen.height/4), normal, "normal"); 
     if (normal) 
     { 
      easy = false; 
      hard = false; 
      Title.difficulty = 2; 
     } 
     hard = GUI.Toggle(new Rect(Screen.width/4, Screen.height * 9/16, Screen.width/2, Screen.height/4), hard, "hard"); 
     if (hard) 
     { 
      normal = false; 
      easy = false; 
      Title.difficulty = 3; 
     } 
     if (easy || normal || hard) 
     { 
      if (GUI.Button(new Rect(Screen.width/4, Screen.height * 13/16, Screen.width/2, Screen.height/8), "Proceed")) 
      { 
       Application.LoadLevel("Home"); 
      } 
     } 
    } 
} 

void Start() { 
    message = "THis is Text for trial slow slow"; 
    text = ""; 
    StartCoroutine(TypeText()); 
} 

void Update() { 
    if (text == message){ 
     textfin = true; 
    } 
} 

IEnumerator TypeText() 
{ 
    foreach (char letter in message.ToCharArray()) 
    { 
     text += letter; 
     yield return new WaitForSeconds(letterPause); 
    } 
} 
+0

我不知道你的變量'textfin'。難道不是跳過按鈕被創建兩次,處於相同的位置......您是否嘗試使用可視化調試器或打印進行調試? – arainone

+0

Opps我將腳本添加到了兩個單獨的對象中......它創建了兩個按鈕,就像你說的那樣...... :( –

回答

3

當我嘗試你的代碼時,點擊跳過按鈕消失。

就你而言,腳本可能會在場景中添加兩次。

另外考慮到Unity 4.6引入了一個您應該使用的新UI系統。見this video

+0

噢,我的上帝......我真的很抱歉,你說的對,我把劇本兩次加到了現場。 .. Oppps。我只是看自己的腳本。對不起,我的錯誤:( –

相關問題