I'm having a problem, I want to make a Switch Case that says who won, who lost or if they drew ( Venceu, Perdeu e Empatou).
I did it first with integer values, with int valueA and valueB, where if someone wins it's worth 1, whoever loses it's 0 and a tie, both are worth 1.
However, it ended up giving the error:
error CS0029: Cannot implicitly convert type 'bool' to '(int valueA, int valueB)'
So I redid using boolean values, 1 = true and 0 = false.
But the error still persists and I don't know how to fix it.
This is my code:
if(Input.GetKey(key1)) // Vence
{
animBlue.SetBool("AzulTiro", true);
valorA = true;
}
else if(animRed.GetBool("VermelhoTiro") == true) // Perde
{
animBlue.SetBool("AzulMorte", true);
valorA = false;
}
if(Input.GetKey(key2)) // Vence
{
animRed.SetBool("VermelhoTiro", true);
valorB = true;
}
else if(animBlue.GetBool("AzulTiro") == true) // Perde
{
animRed.SetBool("VermelhoMorte", true);
valorB = false;
}
switch (valorA, valorB)
{
case (valorA == true && valorB == false):
Debug.Log("Azul Venceu");
break;
case (valorA == false && valorB == true):
Debug.Log("Vermelho Venceu");
break;
case (valorA == true && valorB == true):
Debug.Log("Empatou");
break;
}
I'm pretty sure the error is on the Switch but I don't know what it is
CodePudding user response:
wrong syntax, you need
switch (valorA, valorB)
{
case (true ,false):
Debug.Log("Azul Venceu");
break;
case (false , true):
Debug.Log("Vermelho Venceu");
break;
case (true, true):
Debug.Log("Empatou");
break;
}
CodePudding user response:
How about pattern matching if your C# is modern enough?
var msg = (valorA, valorB) switch {
(true, false) => "Azul Venceu",
(false, true) => "Vermelho Venceu",
_ => "Empatou"
};
Debug.Log(msg);