所以我试图在团结中创建一个简单的文本冒险,我编写了一个脚本,根据按键更改文本的状态。为了尝试添加一些复杂性,我试图添加一个系统,当处于一个状态时,显示一个项目被拾取,并且该项目的变量已设置为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;
}
}
Console saying item is obtained, 'true'
Console saying that the item has not been obtained for some reason, 'false'
答案 0 :(得分:3)
嗯,你的脚本有几个问题。
让我们从这开始:
if (rock = true);{...
你需要意识到你分配了rock = true,你需要使用double equals来比较。更重要的是,建议使用if(boolVar),因为如果它是真的则自动执行。
if(rock){ do something }
//is the same that
if(rock == true){}
另外,另一件重要的事情是,你不应该放一个;在if之后,因为当你这样做时,括号内的代码将不会与条件相关。
答案 1 :(得分:1)
这些陈述存在一些明显的问题。
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
的末尾添加分号,并且=
符号本身会在评估之前将右侧的值与左侧的变量对齐。因此,将单个=
更改为==
。
您的代码变为,
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.";
}