2014-04-14 64 views
0

基本上我在遊戲中製作了一個變量,當玩家通過碰撞機時它會顯示所顯示的消息。不過,如果有人能幫助我,我會犯一些錯誤。我不確定我到底發生了什麼,它變得非常令人沮喪。布爾變量和小編碼錯誤

using UnityEngine; 
using System.Collections; 

public class showMessage : MonoBehaviour { 
    GameObject player; 
    private bool startLap; 

    void OnTriggerEnter (Collider other ){ 
     if(other.tag == "Player") { 
      bool = true; 
     } 
    } 

    void OnGUI(){ 
     if(bool == true) { 
      if(GUI.Button (new Rect(100, 100, 500, 40), "Help! I lost my car. Find it through this maze for me?")) { 
       Debug.Log("Door Works!"); 
       bool = false; 

      } 
     } 


    } 
} 

好固定的BOOL布爾剛纔看到:\

我得到的錯誤是:

(10,30):錯誤CS1525:意外的符號=', expecting',?', [',<operator>', or標識符'

(15,26):錯誤CS1525:意外符號==', expecting。' 錯誤CS1525:意外符號=', expecting。'

錯誤CS8025:分析錯誤

+0

「我有一些錯誤」 - 他們是? –

+1

如果你包含你得到的錯誤,這將有所幫助。 –

+0

@MitchWheat貼這些對不起! – user2980622

回答

3

布爾是布爾值數據類型保留關鍵字 - 不要用它作爲字段名,或者你會得到許多編譯錯誤。將該字段重命名爲其他內容。

你的代碼必須是這樣的:

using UnityEngine; 

public class showMessage : MonoBehaviour 
{ 
    private bool startLap; 

    void OnTriggerEnter(Collider other) 
    { 
     if (other.tag == "Player") 
     { 
      startLap = true; 
     } 
    } 

    void OnGUI() 
    { 
     if (startLap) 
     { 
      if (GUI.Button(new Rect(100, 100, 500, 40), "Help! I lost my car. Find it through this maze for me?")) 
      { 
       Debug.Log("Door Works!"); 
       startLap = false; 
      } 
     } 
    } 
} 
+0

..或使用'@ bool' ...但是可能更好的想法是完全改變它。我見過的廣泛使用關鍵字(帶有前綴「@」)的唯一地方是基於事件的體系結構,其中'@ event'是一個公共變量名。 –

2

private bool startLap;

這個變量的類型bool和名稱startLap

想想看,你可以有很多這樣的變量:

private bool startLap; 
private bool foo; 
private bool bar; 

他們都有不同的名稱,但bool同一類型。

現在在你的代碼,你有

bool = true;

if(bool == true) {

bool = false;

哪個變量被他們指的是?從你的例子,我可以猜到,因爲只有一個bool左右,但由於可能是別人,你必須通過名稱引用的所有變量,而不是鍵入:

startLap= true;

if(startLap== true) {

startLap= false;