If-statements doesnt seem to be working.. (CODE IS CORRECT),Why isnt Unity seeing the if-statements?
I made a game where you can run through levels with highscores. And i made one for any level.. so the part that isnt working, is the part that saves the highscore to playerprefs... For Level 1 its working but not for the two other ones ;( I compared the level 1 if-statement with the two others and they didnt had anything bad... I dont know whats my fault.. Here the if-statement for Level 1:
void OnTriggerExit (Collider player) {
Scene currentScene = SceneManager.GetActiveScene ();
string sceneName = currentScene.name;
float t = Time.time - StartTime;
string seconds = (t % 60).ToString ("f2");
TimerText.text = seconds;
Debug.Log ("Current Scene is: " + sceneName);
if (sceneName == "Level 1" & t < PlayerPrefs.GetFloat ("Record1", 999999) & FINISHTEXT.activeSelf == true) {
PlayerPrefs.SetFloat ("Record1", t);
Record = PlayerPrefs.GetFloat ("Record1", 999999);
RecordText.text = PlayerPrefs.GetFloat ("Record1", 999999).ToString ();
Debug.Log ("LEVEL1 NEW RECORD!");
}
}
And heres the if-statement for Level 2 and three:
void OnTriggerExit (Collider player) {
Scene currentScene = SceneManager.GetActiveScene ();
string sceneName = currentScene.name;
float t = Time.time - StartTime;
string seconds = (t % 60).ToString ("f2");
TimerText.text = seconds;
Debug.Log ("Current Scene is: " + sceneName);
if (sceneName == "Level 2" & t < PlayerPrefs.GetFloat ("Record2", 999999) & FINISHTEXT.activeSelf == true) {
PlayerPrefs.SetFloat ("Record2", t);
Record = PlayerPrefs.GetFloat ("Record2", 999999);
RecordText.text = PlayerPrefs.GetFloat ("Record2", 999999).ToString ();
Debug.Log ("LEVEL2 NEW RECORD!");
}
}
Answer by MT369MT · Aug 12, 2018 at 05:15 PM
Hi, I see that in your if conditions you used & to check if all of your conditions are true. But & is the bitwise "and" operator. In the if coditions you must use && that is the logical "and" operator.
Example:
if (sceneName == "Level 1" && t < PlayerPrefs.GetFloat ("Record1", 999999) && FINISHTEXT.activeSelf == true)
But why is it working in the first if-statement but not in the second? I think i tried the && but it gave me the same result.. Ill try tomorrow :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612172701im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The If statement condition is false but the if statement stills executes 1 Answer
Help with rotation please 2 Answers
weird problem in C# 1 Answer
Add changeable conditions in the editor, similar to unityEvent? 0 Answers
SqrMagnitude intermittently doesn't work for if statement on prefab. (C sharp) 1 Answer