Unity performing both If and Else statements at same time
Recently I saw a strange thing. Unity is reading both if and else statements at same time. Here's the code - void SetupControls() { if(PlayerPrefs.GetString("ShowMM") == "Yes") { Debug.Log(1); } else if(PlayerPrefs.GetString("ShowMM") == "No") { Debug.Log(2); } }
And i am getting both 1 and 2 as log in console. What to do to fix this?? I tried restarting unity and system, nothing worked AND I AM DOUBLE SURE THAT IN PLAYERPREFS IT IS SET TO "Yes"!
I don't know why the code is not being aligned properly
Answer by Artemxd · Feb 04, 2021 at 06:37 PM
Hey!
First of all, visit this link, because it`s unbelievably uncomfortable to read unformatted code. https://forum.unity.com/threads/using-code-tags-properly.143875/
Secondary, Look at the code below, it produces output of one. I guess it`s impossible to run both if-else conditions at the same time. Maybe you output 2 somewhere else?
string s = "example";
if (s == "example")
{
Debug.Log(1);
}
else if (s == "notexample")
{
Debug.Log(2);
}
$$anonymous$$y code is same, I am getting both 1 and 2 in console
Answer by tanmaykulkarni · Feb 05, 2021 at 11:51 AM
I also tried re-installing unity and visual studio, still unity performing both if and else together. I am in a huge trouble, please help!
Change Debug.Log()'s message to "Yes" and "No", and also check that this function is not called twice and this PlayerPreference does not change anywhere
I already checked that things The final solution I got is I was using that code in Start() method in which there were some more commands up. I tried removing them and move that if and else statement above, now it is showing only one message in console. Found the solution, but it is a glitch! Unity should try to fix this.