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 sacajawhoohoo · Nov 04, 2015 at 11:37 PM · scripting problemupdateplayerprefsnot workingelse if

If two PlayerPrefs are on?

So I am having this issue where if I call upon one playerpref it works fine, and if I call upon a different playerpref it also works fine, but if I call upon both of them, it no longer works. I understand what I am saying is confusing, so here is an example:

function Update () { if (PlayerPrefs.GetInt("aPP1")==1) {

     }
     
     else if (PlayerPrefs.GetInt("aPP2")==1)
     {
 
     }
     
     else if ((PlayerPrefs.GetInt("aPP1")==1) && (PlayerPrefs.GetInt("aPP2")==1))
     {
 
     }
     
     else if ((PlayerPrefs.GetInt("aPP1")==0) && (PlayerPrefs.GetInt("aPP2")==0))
     {
 
     }
 }

Now the wierd thing is is that when both playerprefs are set to zero, the last else if works just fine. But if they are both set to "1" the second else if does not work and instead only does the first else if.

How would I go about fixing this?

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
2
Best Answer

Answer by Mako-Infused · Nov 04, 2015 at 11:52 PM

This is a logical problem. You're first checking for a value of 1 from each pref separately, else if it is not then you're checking both of them. That makes no sense, the single check will always return true before you check them both (no fall-through occurs).

The way to fix this would be to put the double checks before the single ones. I'm not sure that I'm explaining well enough, so here is an example:

          if ((PlayerPrefs.GetInt("aPP1")==1) && (PlayerPrefs.GetInt("aPP2")==1))
          {
        
          }
          else if ((PlayerPrefs.GetInt("aPP1")==0) && (PlayerPrefs.GetInt("aPP2")==0))
          {
        
          }
          else if (PlayerPrefs.GetInt("aPP1")==1) 
          {
        
          }
          else if (PlayerPrefs.GetInt("aPP2")==1)
          {
        
          }

The only other option is to restructure this whole statement entirely, which it kind of needs.

Comment
Add comment · Show 5 · 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 sacajawhoohoo · Nov 05, 2015 at 12:22 AM 0
Share

What do you mean by it needs to be restructured as a whole? I will openly admit that I am not the best programmer, so how exactly would you restructure it?

avatar image Mako-Infused sacajawhoohoo · Nov 05, 2015 at 01:40 PM 1
Share

There is no need to admit anything, I think you're doing great. If my statement seemed like a jab at your coding, then I apologize. I was merely pointing out optimization might be warranted.

The restructuring is only kind of needed, by that I meant it would depend on what is contained within the if/else if statements. You could for example do something like this:

 if (PlayerPrefs.GetInt("aPP1")==1 || PlayerPrefs.GetInt("aPP2")==1) 
 {
     if (PlayerPrefs.GetInt("aPP1")==1 && PlayerPrefs.GetInt("aPP2")==1)
     {
         // If both aPP1 and aPP2 are equal to 1
     }
     else
     {
         // If either aPP1 or aPP2 is equal to 1
     }
 }
 else
 {
     // If both aPP1 and aPP2 are not equal to 1
 }

Something like that would reduce redundancy, but would only make sense if your code deals with aPP1 and aPP2 similarly, which I assumed (perhaps a mistake on my part?) that it would. Of course there are other options.

I hope that clears things up!

avatar image Bunny83 Mako-Infused · Nov 05, 2015 at 02:15 PM 0
Share

Your outer else if condition is not needed (given that 0 and 1 are the only values) a simple else will do. Your first if statement is only true when one or both are 1. So if not they both must be 0.

The way your ifs are structured is actually less readable. Just to be clear, i mean the code in your comment, not your answer. Your answer is fine. If you want to make it more readable and reduce redundancy do it like this:

           // C# / UnityScript
           var PP1 = PlayerPrefs.GetInt("aPP1") == 1;
           var PP2 = PlayerPrefs.GetInt("aPP2") == 1;
           if (PP1 && PP2)
           {
         
           }
           else if (PP1) 
           {
         
           }
           else if (PP2)
           {
         
           }
           else // here both are false
           {
               
           }

That way playerprefs are only read once. Also the ifs are more readable

Show more comments
avatar image sacajawhoohoo · Nov 07, 2015 at 05:01 AM 0
Share

Thank you both for helping clear this up.

avatar image
1

Answer by ben-rasooli · Nov 05, 2015 at 12:06 AM

The problem here is the order of your conditions. Let's walk through the code together.

int aPP1 = 1;
int aPP2 = 1;

Now check the first if(). Is aPP1 equal to 1? If your answer is yes(true) then the rest of the condition checks will be ignored. So the order of your condition checks matters.

You need to do it this way:

if ( PlayerPrefs.GetInt("aPP1") == 1 && PlayerPrefs.GetInt("aPP2") == 1 ) {
}else if (PlayerPrefs.GetInt("aPP1") == 1){
}else if (PlayerPrefs.GetInt("aPP2") == 1){
}else if ( PlayerPrefs.GetInt("aPP1") == 0 && PlayerPrefs.GetInt("aPP2") == 0 ){
}
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

35 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

Related Questions

LockCursor script not working as intended 3 Answers

GameObject.FindGameObjectsWithTag not working 0 Answers

Load Script on Game Start 1 Answer

How to prevent Unity to execute all the code in the first frame. 1 Answer

Boolean From Another Class Not Being Updated 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