C# 布尔变量和小的编码错误

C# 布尔变量和小的编码错误,c#,variables,boolean,C#,Variables,Boolean,基本上,我在游戏中制作了一个变量,当一个玩家经过一个对撞机时,它会显示所显示的消息。然而,如果有人能帮助我,我会有一些错误。我不确定我到底哪里出错了,这让我很沮丧 using UnityEngine; using System.Collections; public class showMessage : MonoBehaviour { GameObject player; private bool startLap; void OnTriggerEnter (

基本上,我在游戏中制作了一个变量,当一个玩家经过一个对撞机时,它会显示所显示的消息。然而,如果有人能帮助我,我会有一些错误。我不确定我到底哪里出错了,这让我很沮丧

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 bool刚看到:\

我得到的错误是:

(10,30):错误CS1525:意外符号
=',应为
'、
?'、
['、
'或
标识符'

(15,26):错误CS1525:意外符号
==',应为
' 错误CS1525:意外符号
=',应为
'

错误CS8025:解析错误


bool是布尔值数据类型的保留关键字-不要将其用作字段名,否则会出现许多编译错误。请将此字段重命名为其他名称

您的代码必须如下所示:

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;
            }
        }
    }
}

bool是布尔值数据类型的保留关键字-不要将其用作字段名,否则会出现许多编译错误。请将此字段重命名为其他名称

您的代码必须如下所示:

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;
            }
        }
    }
}

private bool startap;

此变量的类型为
bool
,名称为
startap

考虑到您可能有许多这样的变量:

private bool startLap;
private bool foo;
private bool bar;
它们都有不同的名称,但类型相同
bool

现在在你的代码中,你有

bool=true;

if(bool==true){

bool=false;

他们指的是哪个变量?从您的示例中,我可以猜测,因为只有一个
bool
,但由于可能还有其他变量,您必须按名称而不是类型引用所有变量:

startap=true;

if(startap==true){


startap=false;

private bool startap;

此变量的类型为
bool
,名称为
startap

考虑到您可能有许多这样的变量:

private bool startLap;
private bool foo;
private bool bar;
它们都有不同的名称,但类型相同
bool

现在在你的代码中,你有

bool=true;

if(bool==true){

bool=false;

他们指的是哪个变量?从您的示例中,我可以猜测,因为只有一个
bool
,但由于可能还有其他变量,您必须按名称而不是类型引用所有变量:

startap=true;

if(startap==true){


startap=false;

“我有一些错误”-它们是?@MitchWheat发布了这些错误!@Grantwiney修复了!对不起,我忘记了部分您必须将“bool”更改为“startap”,不仅在该字段声明行。您还必须更改访问该字段的名称(即第11、16和23行)“我有一些错误”-他们是吗?@MitchWheat发布了这些抱歉!@GrantWinney修复了!抱歉我忘记了那个部分您不仅必须在这个字段声明行中将“bool”更改为“startap”。您还必须更改访问它的字段的名称(即第11、16和23行)…或使用
@bool
…但完全改变它可能是一个更好的主意。我见过它被广泛采用的唯一地方是使用关键字(带有
@
前缀)是在基于事件的体系结构中,
@event
是一个常见的变量名…或者使用
@bool
…但是完全更改它可能是一个更好的主意。我所见过的唯一一个被广泛采用的地方是使用关键字(带有
@
前缀)在基于事件的体系结构中,
@event
是一个通用变量名。