简体   繁体   English

如果语句不起作用Unity C#

[英]If statement not working Unity C#

EDIT: Yeah, I have been coding in python lately and iv mixed up the syntax between the two languages. 编辑:是的,最近我一直在用python进行编码,并且iv混合了两种语言之间的语法。 Thanks for the improvement tips. 感谢您的改进提示。

My goal is to get the if statements working and output the corresponding normal, medium and hard. 我的目标是使if语句正常工作并输出相应的normal,medium和hard。 Eventhough GameDifficulty is set to 2, it outputs hard instead of medium. 即使将GameDifficulty设置为2,它也会输出“ hard”而不是“ medium”。 I have noticed that it just outputs the last if statement bit. 我注意到它只输出最后的if语句位。 Its really weird. 真的很奇怪

I define GameDifficulty as 2. When i run the script, Text11 becomes "hard" instead of Medium. 我将GameDifficulty定义为2。运行脚本时,Text11变为“ hard”,而不是Medium。 I have no idea why this happens. 我不知道为什么会这样。

Thank you 谢谢

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class DifficultyDisplayOnWinScreen : MonoBehaviour {
    public int GameDifficulty;
    public GameObject Text11;
    public string diffi;
    void Start () 
    {

        //GameDifficulty = DifficultyChooser.DifficultyNumber;
        GameDifficulty = 2;

    }

    void Update () 
    {
        //GameDifficulty = DifficultyChooser.DifficultyNumber;
        if (GameDifficulty == 3);
        {
            Text11.GetComponent<Text> ().text = "Normal";
        }       
        if (GameDifficulty == 2);
        {
            Text11.GetComponent<Text> ().text = "Medium";
        }
        if (GameDifficulty == 1);
        {
            Text11.GetComponent<Text> ().text = "Hard";
        }

    }
}

Remove the ; 删除; after the if -conditions: if -condition之后:

if (GameDifficulty == 3)
{
    Text11.GetComponent<Text>().text = "Normal";
}
if (GameDifficulty == 2)
{
    Text11.GetComponent<Text>().text = "Medium";
}
if (GameDifficulty == 1)
{
    Text11.GetComponent<Text>().text = "Hard";
}

Explanation: the ; 说明: ; ends the if statement, making it an empty statement. 结束if语句,使其成为空语句。 This is because one-line if statements are allowed in C#, and the ; 这是因为在C#中允许单行if语句,以及; determines where they end. 确定它们在哪里结束。

EDIT : I didn't even notice the ; 编辑 :我什至没注意到; s, yeah remove semicolons and it should work. s,是的,删除分号就可以了。

if (GameDifficulty == 3)
{
    Text11.GetComponent<Text>().text = "Normal";
}       
if (GameDifficulty == 2)
{
    Text11.GetComponent<Text>().text = "Medium";
}
if (GameDifficulty == 1) //if there is only 3 difficulties in this line you can use just else
{
    Text11.GetComponent<Text>().text = "Hard";
}

Or use if/else 或使用if/else

if (GameDifficulty == 3)
{
    Text11.GetComponent<Text>().text = "Normal";
}       
else if (GameDifficulty == 2)
{
    Text11.GetComponent<Text>().text = "Medium";
}
else if (GameDifficulty == 1) //if there is only 3 difficulties in this line you can use just else
{
    Text11.GetComponent<Text>().text = "Hard";
}

and in this case better use switch/case : 在这种情况下,最好使用switch/case

switch(GameDifficulty)
{
    case 1:
        Text11.GetComponent<Text>().text = "Hard";
        break;

    case 2:
        Text11.GetComponent<Text>().text = "Medium";
        break;

    case 3:
        Text11.GetComponent<Text>().text = "Normal";
        break;
}

or you can have a dictionary of difficulties for example: 或者您也可以使用困难字典,例如:

Dictionary<int, string> difficulties = new Dictionary<int, string>()
{
    {1, "Hard" },
    {2, "Medium" },
    {3, "Normal" }
};

and use it like: 并像这样使用它:

Text11.GetComponent<Text>().text = difficulties[GameDifficulty];

Or enum (there is tons of ways to make it more readable and simple, so I will end my examples with this one) 还是enum (有很多方法可以使它更具可读性和简单性,所以我将以这一示例作为结束)

enum Difficulties
{
    Hard = 1,
    Medium = 2,
    Normal = 3
}

usage: 用法:

Text11.GetComponent<Text>().text = ((Difficulties)GameDifficulty).ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM