How to collect all the values of (if) in one ?
I have C# code where there are many conditional if statements. How can i collect all the values of in one ?
void Update () {
if(Score.score == 10)
{
X.text = "X" + 1;
anim.Play("X");
}
if(Score.score == 30)
{
X.text = "X" + 2;
anim.Play("X");
}
if(Score.score == 50)
{
X.text = "X" + 3;
anim.Play("X");
}
if(Score.score == 70)
{
X.text = "X" + 4;
anim.Play("X");
}
if(Score.score == 90)
{
X.text = "X" + 5;
anim.Play("X");
}
}
Answer by Zaeran · Nov 07, 2019 at 01:44 PM
For this particular case, you can use
if(Score.score % 20 == 10)
That will let you know if a score is a 10 higher than a multiple of 20 (10, 30, 50, etc.)
You can then just use integer division to set the X.text value.
X.text = "X" + ((Score.score / 20) + 1)
This will set the value to score / 20 with no remainder (10 / 20 = 0. 30 / 20 = 1, etc.), and adds one to get the value you're after.
In full, it should look like this:
if(Score.score % 20 == 10){
X.text = "X" + ((Score.score / 20) + 1)
anim.Play("X");
}
Answer by Marioooo · Nov 07, 2019 at 01:43 PM
Hello!
if statements are used to do something if a condition is accomplished...
ifs and else is used for certain cases like yours this way:
if (condition)
{
do something
}
else
{
do something
}
if you want to concat multiple if Statements then do this:
if (condition)
{
do something
}
else if (condition)
{
do something
}
else
{
do something
}
this way if some of the above ifs occurs, the code beneath the if statement that accomplished will be executed... if no other if statement is true, then the code on "else" will be executed....
i will do your code like this:
void Update ()
{
if (Score.score == 10)
{
X.text = "X" + 1;
anim.Play("X");
}
else if (Score.score == 30)
{
X.text = "X" + 2;
anim.Play("X");
}
else if (Score.score == 50)
{
X.text = "X" + 3;
anim.Play("X");
}
else if (Score.score == 70)
{
X.text = "X" + 4;
anim.Play("X");
}
else if (Score.score == 90)
{
X.text = "X" + 5;
anim.Play("X");
}
}
you don't have an else statement because you don't have a default behaviour... so you don't need it...
NOW.... idk why you are doing this, you must play an animation when a player reaches a score of 10, 30, 50, 70 or 90? Idk where do you change the score, but is better practice to apply this changes there and play the animation there instead of using update
good luck!
Answer by petur · Nov 07, 2019 at 01:47 PM
I think you are looking for a "switch" statement: https://www.dotnetperls.com/switch
However, I would have use a more general case checking if score is between 1 and 90 and (score - 10) is a multiple of 20 (so 10, 30, 50, 70 and 90), and then using the score value to update the label.
It is not as legible as your series of ifs or a switch, but it is easier to add or change values.
if ( Score.score > 0 && Score.score <= 90 && ( Score.score + 10) % 20 == 0 )
{
x.text = "X" + ( (Score.score + 10) / 20 );
anim.Play("X");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612214501im_/https://answers.unity.com/themes/thub/images/avi.jpg)