如何在if语句中使用布尔变量更改文本(C#)

如何在if语句中使用布尔变量更改文本(C#),c#,unity3d,boolean,C#,Unity3d,Boolean,所以我试图在unity中创建一个简单的文本冒险,我已经编写了一个脚本,根据按键的不同来改变文本的状态。为了尝试增加一点复杂性,我尝试添加一个系统,其中在一种状态下,显示一个项目已被拾取,并且该项目的变量已设置为true,这在脚本顶部说明。然后,当用户下一步进入不同的状态时,将检查变量是否为真,如果变量为真,则将显示不同的文本,如果变量为真,则显示不同的文本。我的问题是,每次我用IF语句进入状态时,不管发生什么,变量都会自动设置为false。如果您能给我一个答案,我将不胜感激:) 引擎: usin

所以我试图在unity中创建一个简单的文本冒险,我已经编写了一个脚本,根据按键的不同来改变文本的状态。为了尝试增加一点复杂性,我尝试添加一个系统,其中在一种状态下,显示一个项目已被拾取,并且该项目的变量已设置为true,这在脚本顶部说明。然后,当用户下一步进入不同的状态时,将检查变量是否为真,如果变量为真,则将显示不同的文本,如果变量为真,则显示不同的文本。我的问题是,每次我用IF语句进入状态时,不管发生什么,变量都会自动设置为false。如果您能给我一个答案,我将不胜感激:)

引擎:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TextController : MonoBehaviour {

public Text text;
private enum states {cell, floor, CellDoor, mirror, bed, rock, shard};
private states myState;
bool rock;
bool shard;


    // Use this for initialization
    void Start () {
        rock = false;
        shard = false;
        myState = states.cell; 
    }

    // Update is called once per frame
    void Update () {    
        print (myState);
        if (myState == states.cell){
            state_cell ();
        }else if (myState == states.floor){
            state_floor ();
        }else if (myState == states.CellDoor){
            state_CellDoor ();
        }else if (myState == states.mirror){
           state_mirror ();
        }else if (myState == states.bed){
            state_bed ();
        }else if (myState == states.rock){
            state_rock ();
        }else if (myState == states.shard){
            state_shard ();
        }
    }
void state_rock (){
        rock = true;
        text.text = "You conjure what strength you have and eagerly pry at a slightly loose looking part of the concrete " +
                    "with your blistered hands. Finally a piece breaks free, you drop it into one of your pockets.\n\n" +
                    "You have obtained a rock! \n\n" +
                    "Press R to return to the cell.";               
    if (Input.GetKeyDown(KeyCode.R)){
        myState = states.cell;
    }
    print(rock);
void state_mirror (){
    print (rock);
    if (rock = true);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You remember the hefty chunk of concrete in your pocket. \n\n" +  
                    "Press T if you would like to throw the rock at the mirror or R if you would like to return to the cell";
    }
    if (rock = false);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You are unable to think of any way of shattering the mirror " +
                    "as it is rigidly screwed into the wall, perhaps a blunt object would do the trick?. \n\n" +
                    "Press R to return to the cell.";
    }                           
    if (Input.GetKeyDown(KeyCode.R)){
        myState = states.cell;
    }
    if (Input.GetKeyDown(KeyCode.T)){
        myState = states.shard;
    }

}
变量更改:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TextController : MonoBehaviour {

public Text text;
private enum states {cell, floor, CellDoor, mirror, bed, rock, shard};
private states myState;
bool rock;
bool shard;


    // Use this for initialization
    void Start () {
        rock = false;
        shard = false;
        myState = states.cell; 
    }

    // Update is called once per frame
    void Update () {    
        print (myState);
        if (myState == states.cell){
            state_cell ();
        }else if (myState == states.floor){
            state_floor ();
        }else if (myState == states.CellDoor){
            state_CellDoor ();
        }else if (myState == states.mirror){
           state_mirror ();
        }else if (myState == states.bed){
            state_bed ();
        }else if (myState == states.rock){
            state_rock ();
        }else if (myState == states.shard){
            state_shard ();
        }
    }
void state_rock (){
        rock = true;
        text.text = "You conjure what strength you have and eagerly pry at a slightly loose looking part of the concrete " +
                    "with your blistered hands. Finally a piece breaks free, you drop it into one of your pockets.\n\n" +
                    "You have obtained a rock! \n\n" +
                    "Press R to return to the cell.";               
    if (Input.GetKeyDown(KeyCode.R)){
        myState = states.cell;
    }
    print(rock);
void state_mirror (){
    print (rock);
    if (rock = true);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You remember the hefty chunk of concrete in your pocket. \n\n" +  
                    "Press T if you would like to throw the rock at the mirror or R if you would like to return to the cell";
    }
    if (rock = false);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You are unable to think of any way of shattering the mirror " +
                    "as it is rigidly screwed into the wall, perhaps a blunt object would do the trick?. \n\n" +
                    "Press R to return to the cell.";
    }                           
    if (Input.GetKeyDown(KeyCode.R)){
        myState = states.cell;
    }
    if (Input.GetKeyDown(KeyCode.T)){
        myState = states.shard;
    }

}
不起作用的语句:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TextController : MonoBehaviour {

public Text text;
private enum states {cell, floor, CellDoor, mirror, bed, rock, shard};
private states myState;
bool rock;
bool shard;


    // Use this for initialization
    void Start () {
        rock = false;
        shard = false;
        myState = states.cell; 
    }

    // Update is called once per frame
    void Update () {    
        print (myState);
        if (myState == states.cell){
            state_cell ();
        }else if (myState == states.floor){
            state_floor ();
        }else if (myState == states.CellDoor){
            state_CellDoor ();
        }else if (myState == states.mirror){
           state_mirror ();
        }else if (myState == states.bed){
            state_bed ();
        }else if (myState == states.rock){
            state_rock ();
        }else if (myState == states.shard){
            state_shard ();
        }
    }
void state_rock (){
        rock = true;
        text.text = "You conjure what strength you have and eagerly pry at a slightly loose looking part of the concrete " +
                    "with your blistered hands. Finally a piece breaks free, you drop it into one of your pockets.\n\n" +
                    "You have obtained a rock! \n\n" +
                    "Press R to return to the cell.";               
    if (Input.GetKeyDown(KeyCode.R)){
        myState = states.cell;
    }
    print(rock);
void state_mirror (){
    print (rock);
    if (rock = true);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You remember the hefty chunk of concrete in your pocket. \n\n" +  
                    "Press T if you would like to throw the rock at the mirror or R if you would like to return to the cell";
    }
    if (rock = false);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You are unable to think of any way of shattering the mirror " +
                    "as it is rigidly screwed into the wall, perhaps a blunt object would do the trick?. \n\n" +
                    "Press R to return to the cell.";
    }                           
    if (Input.GetKeyDown(KeyCode.R)){
        myState = states.cell;
    }
    if (Input.GetKeyDown(KeyCode.T)){
        myState = states.shard;
    }

}


嗯,您的脚本中有几个问题

让我们从以下内容开始:

 if (rock = true);{...
你需要意识到你被赋值为rock=true,你需要使用double-equals来进行比较。更重要的是,建议使用if(boolVar),因为如果它是真的,将自动执行

if(rock){ do something }
//is the same that 
if(rock == true){}

还有,另一件重要的事,就是你不应该把;在if之后,因为这样做时括号内的代码将与条件无关。

好吧,脚本中有几个问题

让我们从以下内容开始:

 if (rock = true);{...
你需要意识到你被赋值为rock=true,你需要使用double-equals来进行比较。更重要的是,建议使用if(boolVar),因为如果它是真的,将自动执行

if(rock){ do something }
//is the same that 
if(rock == true){}

还有,另一件重要的事,就是你不应该把;在if之后,因为这样做时括号内的代码将与条件无关。

这些语句存在一些明显的问题

if (rock = true);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You remember the hefty chunk of concrete in your pocket. \n\n" +  
                    "Press T if you would like to throw the rock at the mirror or R if you would like to return to the cell";
    }
    if (rock = false);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You are unable to think of any way of shattering the mirror " +
                    "as it is rigidly screwed into the wall, perhaps a blunt object would do the trick?. \n\n" +
                    "Press R to return to the cell.";
    }            
如果,则不要在
的末尾加分号,
=
符号本身会在计算变量之前将右侧的值赋给左侧的变量。因此,将单个
=
更改为
=

你的代码变成

if (rock == true){
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You remember the hefty chunk of concrete in your pocket. \n\n" +  
                    "Press T if you would like to throw the rock at the mirror or R if you would like to return to the cell";
    }
    if (rock == false){
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You are unable to think of any way of shattering the mirror " +
                    "as it is rigidly screwed into the wall, perhaps a blunt object would do the trick?. \n\n" +
                    "Press R to return to the cell.";
    }        
虽然写这篇文章的更好方法是

if (rock) // is the same as if rock == true
{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You remember the hefty chunk of concrete in your pocket. \n\n" +  
                    "Press T if you would like to throw the rock at the mirror or R if you would like to return to the cell";    
}
else // is the same as if rock == false (in this case)
{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You are unable to think of any way of shattering the mirror " +
                    "as it is rigidly screwed into the wall, perhaps a blunt object would do the trick?. \n\n" +
                    "Press R to return to the cell.";    
}

这些说法显然存在一些问题

if (rock = true);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You remember the hefty chunk of concrete in your pocket. \n\n" +  
                    "Press T if you would like to throw the rock at the mirror or R if you would like to return to the cell";
    }
    if (rock = false);{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You are unable to think of any way of shattering the mirror " +
                    "as it is rigidly screwed into the wall, perhaps a blunt object would do the trick?. \n\n" +
                    "Press R to return to the cell.";
    }            
如果
,则不要在
的末尾加分号,
=
符号本身会在计算变量之前将右侧的值赋给左侧的变量。因此,将单个
=
更改为
=

你的代码变成

if (rock == true){
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You remember the hefty chunk of concrete in your pocket. \n\n" +  
                    "Press T if you would like to throw the rock at the mirror or R if you would like to return to the cell";
    }
    if (rock == false){
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You are unable to think of any way of shattering the mirror " +
                    "as it is rigidly screwed into the wall, perhaps a blunt object would do the trick?. \n\n" +
                    "Press R to return to the cell.";
    }        
虽然写这篇文章的更好方法是

if (rock) // is the same as if rock == true
{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You remember the hefty chunk of concrete in your pocket. \n\n" +  
                    "Press T if you would like to throw the rock at the mirror or R if you would like to return to the cell";    
}
else // is the same as if rock == false (in this case)
{
        text.text = "A rather old fashioned mirror, strangely clean and drilled into the wall. Shattering the mirror and " +
                    "wielding a shard could prove useful. You are unable to think of any way of shattering the mirror " +
                    "as it is rigidly screwed into the wall, perhaps a blunt object would do the trick?. \n\n" +
                    "Press R to return to the cell.";    
}

=
=
之间有区别,我建议你多读一些基本的C#concepts
if(rock=true);{
在该行中,分号终止if语句。结束它。它意味着
if
控制一个空语句——这意味着if要么执行一个空语句(不执行任何操作),要么不执行该空语句(也不执行任何操作)。后面用大括号括起来的块与
if
无关。它只是执行。除了修复
=
的错误用法外,还需要去掉分号。由于
声明
是一个枚举,因此更需要将
Update
方法更改为使用
开关而不是序列在
=
=
之间有一个区别,我建议你多读一些基本的C#概念
如果(rock=true);{
在该行中,分号终止if语句。结束它。它意味着
if
控制一个空语句——这意味着if要么执行一个空语句(不执行任何操作),要么不执行该空语句(也不执行任何操作)。后面用大括号括起来的块与
if
无关。它只是执行。除了修复
=
的错误用法外,还需要去掉分号。由于
声明
是一个枚举,因此更需要将
Update
方法更改为使用
开关而不是序列我意识到这是多么的新手错误,谢谢!我意识到这是多么的新手错误,谢谢!