- Home /
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?
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.
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?
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!
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
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 ){
}