簡體   English   中英

如何在if語句中使用布爾變量來更改文本(C#)

[英]How to use a Boolean variable in an if statement to change text (C#)

因此,我試圖統一創建一個簡單的文本冒險,並且我編寫了一個腳本,該腳本根據按鍵的不同來更改文本的狀態。 為了嘗試增加一點復雜性,我嘗試添加一個系統,在該系統中,當處於一種狀態時,將顯示一個項目已被拾取,並且該項目的變量已設置為true(在腳本頂部列出)。 然后,當用戶下一次進入其他狀態時,將檢查該變量是true還是false,其中如果變量為true,則將顯示不同的文本,如果變量為false。 我的問題是,每次我使用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;
    }

}

控制台說項目獲得,“ true”

控制台表示由於某種原因未獲得該項目,即“ false”

好吧,您的腳本中有幾個問題。

讓我們以此開始:

 if (rock = true);{...

您需要認識到分配的岩石= true,需要使用雙等於進行比較。 甚至建議使用if(boolVar),因為如果為true,則會自動執行。

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的末尾放置分號,而=符號本身會將右邊的值賦給左邊的變量,然后對其進行評估。 因此,將單=更改為a ==

您的代碼變成

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM