Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DarkyneMB · Jun 20, 2017 at 03:51 AM · c#if-statementssimple

How can i simplify multiple if-statements?

Hey guys, i'm working on a small game currently and was working on a function to allow multiple languages. My script works very well but i'm wondering if i can simplify my script a little.

     public void SetLanguage()
     {
         language = PlayerPrefs.GetInt("language");
         for (int i = 0; i < Object.Length; i++)
         {
             if (language == 0)
             {
                 Object[i].ObjectText.text = Object[i].enString;
             }
             else if (language == 1)
             {
                 Object[i].ObjectText.text = Object[i].deString;
             }
         }
     }

if i would add more languages i would have to check for "else if (language == 2) , else if language == 3), etc...) is there a way to shorten the script?

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 sparkzbarca · Jun 20, 2017 at 04:02 AM

yep, do a string array and do .textstring[x] have that set by possibly even a global as that might actually make sense here that's an enum

so what you get roughly is

enum Language { English, German, French}

so what that means is

Language languageChoice = 0 //this is english, 0 is english, 1 is german, 2 is french however

language langaugeChoice = Langauge.English //also works, it's just a way of saying zero but in a much more readable not going to get screwed by it later when you make changes and add a language or something kind of way.

so what you can then do with this is because you know 0 is english, 1 german, 2 french you go

string[] text;

text = new string[3];

text[0] = //english text; text[1] = //german; text[2] = //french;

then Object[i].text[Languagechoice];

so now you simply ask the player what language they want, store that result in the Language choice variable and now you only have to call text[langaugechoice] and if they change from english to german, your text will change.

Comment
Add comment · Show 3 · 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 DarkyneMB · Jun 20, 2017 at 04:14 AM 0
Share

Excuse me, could you explain it more detailed please? Never worked with an enum

avatar image Woltus · Jun 20, 2017 at 07:36 AM 0
Share

@Darkyne$$anonymous$$B check here ;) https://unity3d.com/learn/tutorials/topics/scripting/enumerations

avatar image sparkzbarca · Jun 22, 2017 at 03:18 AM 0
Share

Enums are just like say how with phones each number has a few letters associated with it and you can tell someone to dial 1800 lawyer ins$$anonymous$$d of the numbers because it's easy to remember.

An enum is just an int, just a number but you can get that by using a word. $$anonymous$$uch like you can look up a number with a name.

Emums aren't strictly needed, you could just use an int array, however if you do that you need to always remember that 0 is English, 1 is Spanish etc.

In unity an example is tags or layers.

Tags and layers are enums. When you place something in a layer or tag its actually a number, the name is so Insta of remembering that 0 is say terrain you can ins$$anonymous$$d put layer.terrain now layer.terrain IS zero but it's nice for reading code and writing it to put layer.terrain, this is purely a human convenience thing though, it will go through the compiler and come out zero.

But when you go later and read code or someone else does reading layer.terrain tells you a lot more about what layer your using than 0 just like later reading

Array [Language.english] makes much more clear what your code does than array[0]

Even if they mean the same thing.

So like using health =100 and putting health helps ins$$anonymous$$d of 100 using enums helps.

But they are at bottom arrays of ints

avatar image
0

Answer by thedigitalminer · Jun 22, 2017 at 01:43 PM

I really like enum's for readability. I would be inclined to do something like this, but obviously edited slightly to work with your script :)

     public enum PlayerLanguage
     {
         English,
         German,
         Spanish
     }
     
     class MyClass : MonoBehaviour
     {
         PlayerLanguage playerLanguage;
 
         public void SetLanguage()
         {
             playerLanguage = (PlayerLanguage)PlayerPrefs.GetInt("language");
 
             for (int i = 0; i < Object.Length; i++) {
                 switch (PlayerLanguage) {
                 case PlayerLanguage.English:
                     Object [i].ObjectText.text = Object [i].enString;
                     break;
                 case PlayerLanguage.German:
                     Object [i].ObjectText.text = Object [i].deString;
                     break;
                 case PlayerLanguage.Spanish:
                     Object [i].ObjectText.text = Object [i].esString;
                     break;
                 default:
                     break;
                 }
             }
         }
     }
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

330 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Input.GetKey(KeyCode.E) requires multiple presses. 1 Answer

If statement behaves unexpectedly 3 Answers

Simplify Code 3 Answers


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