- Home /
if statement for 6 ints help C#
Hey guys in the script below I have 6 integers and looking on how to do an if statement so that whichever one is the highest. I've done if statements before like if number is higher or lower than 50 for example, but nothing like to compare 6 different integers
public class MentalStateManager : MonoBehaviour
{
public static MentalStateManager instance;
public int neutral = 0;
public int direct = 0;
public int personalResponsibility = 0;
public int nurturing = 0;
public int distracting = 0;
public int damaging = 0;
void Awake()
{
instance = this;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
neutral = (neutral + 1);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
direct = (direct + 1);
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
personalResponsibility = (personalResponsibility + 1);
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
nurturing = (nurturing + 1);
}
if (Input.GetKeyDown(KeyCode.Alpha5))
{
distracting = (distracting + 1);
}
if (Input.GetKeyDown(KeyCode.Alpha6))
{
damaging = (damaging + 1);
}
}
}
pretty much I have a new script called that will change scenes depending on which ever is the highest and just need help to write the if statement only the rest I can do. So like if neutral is the highest play this scene, if direct is the highest play this scene for example.This is all I got so far for the ChangeScenes script. Thanks for the help guys.
public class SceneChange : MonoBehaviour
{
void OnTriggerEnter (Collider col)
{
if(col.tag == "Player" && MentalStateManager.instance.neutral >
}
}
Answer by hbalint1 · Apr 24, 2015 at 05:09 PM
after you calculated all the 6 integers and need to change scene, you should put all the 6 integers in an array. You can get the max values of them with: http://docs.unity3d.com/ScriptReference/Mathf.Max.html Aftre you got the max, you can change the scene for whichever you like. Like:
public class SceneChange : MonoBehaviour
{
void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
int[] array = new int[] { neutral, direct, personalResponsibility, nurturing, distracting, damaging };
int max = Mathf.Max(array);
Application.LoadLevel(max);
}
}
}
Answer by Bunny83 · Apr 24, 2015 at 05:43 PM
IF you need to perform such an analyse on those values you really should replace them in an array or this is going to be messy.
This would be one simplification:
public int[] values = new int[6];
void Update()
{
for (int i = 0; i < values.Length; i++)
{
if (Input.GetKeyDown(KeyCode.Alpha1+i))
{
values[i]++;
}
}
}
public int FindMax()
{
int currentV = -1;
int currentIndex = 0;
for(int i = 0; i < values.Length; i++)
{
if (values[i]> currentV)
{
currentIndex = i;
currentV = values[i];
}
}
return currentIndex;
}
So instead of having 6 individual integers we use one array with 6 values.
FindMax() will return the index of the array element which holds the largest value.
You could define another array with level names which should be loaded for each value:
public string[] levelNames;
Fill in the 6 level names in the inspector. You can get the level name like this:
int index = FindMax();
string levelName = levelNames[index];
Since it's easy to mess up those two arrays (you always need as many level names as integer values in the other array), it might be useful to bundle your value with the level name and maybe even a string that describes the value in case you want to show those 6 values to the user:
[System.Serializable]
public class CustomValue
{
public string Name; // name of the value, could be "neutral", "direct", ...
public int Value; // the actual value
public string LevelName; // will hold the level name associated with this value
}
// edit those in the inspector. Set the size to 6 and fill in the names and the
// associated level names.
public CustomValue[] values;
void Update()
{
for (int i = 0; i < values.Length; i++)
{
if (Input.GetKeyDown(KeyCode.Alpha1+i))
{
values[i].Value++;
}
}
}
public CustomValue FindMax()
{
CustomValue current = values[0];
for(int i = 1; i < values.Length; i++)
{
if (values[i].Value > current.Value)
current = values[i];
}
return current;
}
Now FindMax returns an instance of custom class. This has several advantages. We can directly access the Value. it's Name and the LevelName.
string levelName = FindMax().LevelName;
Now the value is directly coupled with the level name that should be loaded.
Your answer
Follow this Question
Related Questions
Problem with IF with 2 outputs. 1 Answer
Need Help Improving If Else statement 1 Answer
Create DoTween from for loop? 1 Answer
IF - ELSE Issue! (Help! I'm new to this) 1 Answer
Problem with On/Off Switch 1 Answer