- Home /
Advice to make a lots of IF statements into a single FOR statement.
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.
This will probably never work because of float precision. YOu'd rather clamp the value if you want to see it happening.
Even if you are using Int, Unity is still using float for the transform.position, which has floating point precision and may not be guaranteed to be a whole number. Just keep it in $$anonymous$$d.
Answer by GameVortex · Jan 06, 2014 at 11:05 AM
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[i])
{
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.
Thanks a lot guys, GameVortex and flamy. You guys helped me a lot and saved me a bunch of space hahahaha.
Answer by flamy · Jan 06, 2014 at 11:06 AM
Yes it is possible and that is how it should be done.
Look in to examples of how For works. Check this and this link.
In your case this will be the following.
for (int i=0;i<positions.Length;i++)
{
if(player1.transform.position == positions[i])
{
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])
{
Debug.Log("Player1 esta na casa 1.");
Turn.player1CaiuNaCasa = false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613125656im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Check for collision in for loop c# 1 Answer
For loop has 'if-else' impossibility. 2 Answers
Exiting an if statement when condition has been met 1 Answer
Read Array Inside Array 0 Answers
How do I check if all objects in an array are destroyed? 2 Answers