Hello guys. This is my first question in UnityAnswers :).I’m a beginner with programming but i’m trying to improve it.
I gonna show the code(JavaScript) and then I make my question.
if(player1.transform.position == positions[0])
{
Debug.Log("Player1 esta no Inicio.");
Turn.player1CaiuNaCasa = false;
}
if(player1.transform.position == positions[1])
{
Debug.Log("Player1 esta na casa 1.");
Turn.player1CaiuNaCasa = false;
}
if(player1.transform.position == positions[2])
{
Debug.Log("Player1 esta na casa 2.");
Turn.player1CaiuNaCasa = false;
}
if(player1.transform.position == positions[3])
{
Debug.Log("Player1 esta na casa 3.");
Turn.player1CaiuNaCasa = false;
}
if(player1.transform.position == positions[4])
{
Debug.Log("Player1 esta na casa 4.");
Turn.player1CaiuNaCasa = false;
}
if(player1.transform.position == positions[5])
{
Debug.Log("Player1 esta na casa 5.");
Turn.player1CaiuNaCasa = false;
}
if(player1.transform.position == positions[6])
{
Debug.Log("Player1 esta na casa 6.");
Turn.player1CaiuNaCasa = false;
}
if(player1.transform.position == positions[7])
{
Debug.Log("Player1 esta na casa 7.");
Turn.player1CaiuNaCasa = false;
}
I have over 30 if statements like the one above.
I wanna know if there is any possibility to make a for statement to do the same thing for all my ifs? Or any other thing to make it smaller.
Thanks all for helping me. Sorry about my english, I’m brasilian.
Yes it is possible and that is how it should be done.
Look in to examples of how For works. Check [this][1] and [this][2] link.
In your case this will be the following.
for (int i=0;i<positions.Length;i++)
{
if(player1.transform.position == positions*)*
{ Debug.Log(“Player1 esta na casa “+i+”.”); Turn.player1CaiuNaCasa = false; } } or you can use foreach if you dont need the index of wat position is… foreach (Vector3 position in positions) { if(player1.transform.position == position) { //Do something for position… Turn.player1CaiuNaCasa = false; } } BTW when ever you use multiple if statements make sure that you are using a if…else if(player1.transform.position == positions[0]) {
Debug.Log(“Player1 esta no Inicio.”); Turn.player1CaiuNaCasa = false; }
if(player1.transform.position == positions[1]) {
Debug.Log(“Player1 esta na casa 1.”); Turn.player1CaiuNaCasa = false; } the above one is less efficient than if(player1.transform.position == positions[0]) {
Debug.Log(“Player1 esta no Inicio.”); Turn.player1CaiuNaCasa = false; } else if(player1.transform.position == positions[1]) {
That is fairly easy because you are using the same objects and doing the exact same thing in all the if statements. The for loop could look something like this:
for(int i = 0; i < positions.Length; i++)
{
if(player1.transform.position == positions*)*
{ Debug.Log("Player1 esta na casa " + i); Turn.player1CaiuNaCasa = false; } } We use the positions.Length as the value that determines how many times the loop should run. If you want it to run a specified number of times instead of the length of the array, you can put that specific amount there instead of positions.Length. Make sure you notice that we used the counter value ‘i’ to get the value from the array. It is also in the Debug.Log to show the correct value. A final note is that it is usually a bad idea to compare positions directly because of how Unity handles position and float values, it is unlikely that the values will be the same. This of course depends on the rest of your implementation and how you want it to work.