Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mightycam · Apr 24, 2015 at 04:55 PM · ontriggerenterif-statementsifintegerelse

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 > 
     }
 }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges