Reputation:
EDIT: Yeah, I have been coding in python lately and iv mixed up the syntax between the two languages. Thanks for the improvement tips.
My goal is to get the if statements working and output the corresponding normal, medium and hard. Eventhough GameDifficulty is set to 2, it outputs hard instead of medium. I have noticed that it just outputs the last if statement bit. Its really weird.
I define GameDifficulty as 2. When i run the script, Text11 becomes "hard" instead of 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";
}
}
}
Upvotes: 1
Views: 1973
Reputation: 3108
Remove the ;
after the if
-conditions:
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. This is because one-line if
statements are allowed in C#, and the ;
determines where they end.
Upvotes: 6
Reputation: 7213
EDIT: I didn't even notice the ;
s, yeah remove semicolons and it should work.
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 (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(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 Difficulties
{
Hard = 1,
Medium = 2,
Normal = 3
}
usage:
Text11.GetComponent<Text>().text = ((Difficulties)GameDifficulty).ToString();
Upvotes: 4