Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Check if all codes are valid

Discussion in 'Scripting' started by LuryannC, Aug 17, 2022.

  1. LuryannC

    LuryannC

    Joined:
    Feb 24, 2022
    Posts:
    11
    Hi I have a code input system, and a "string[]" with all my codes and I used a for each loop to iterate through all of them and check if the provided code is equal to one of the codes in the list.

    When the code is it opens the landing page, and when it's not it displays an "Invalid code" message.

    The problem is, I have my list, for example ("123456", "654321", "112233") and If I write the last code, the "Invalid Code" message will be displayed and after a few seconds, it will open the landing page as the code is valid.
    When the code is not valid it works fine and just shows the "Invalid Code" message.

    My code used on the submit button:

    Code (CSharp):
    1.    
    2. public void UseCode(string customPin = null)
    3.     {  
    4.         int validCode = 0;
    5.         string inputPin = null;
    6.         if(string.IsNullOrEmpty(customPin))
    7.         {
    8.             inputPin = InputFieldActions.TrimPadding(inputField.text);
    9.         }
    10.         else
    11.         {
    12.             inputPin = customPin;
    13.         }
    14.         if (IsValid(usersPincodes, inputPin))
    15.         {
    16.             //pincodePage.SetActive(false);
    17.             pincodePage.GetComponent<Animator>().SetBool("Invalid", false);
    18.             pincodePage.GetComponent<Animator>().SetBool("Visible", false);
    19.             menu.GetComponent<Animator>().SetBool("Default", true);    
    20.         }
    21.         else
    22.         {
    23.             // If pin is invalid load Invalid Pin message
    24.             pincodePage.GetComponent<Animator>().SetBool("Invalid", true);
    25.         }
    26.     }
    27.  
    28.  
    The code to check if it's valid:

    Code (CSharp):
    1.     bool IsValid(string[] codeList, string pin)
    2.     {
    3.         int check = 0;
    4.         foreach (string code in usersPincodes)
    5.         {
    6.             if(code == pin)
    7.             {
    8.                 dataTracking.instance.userCode = patients[code];
    9.                 welcomeText.text = "Welcome " + dataTracking.instance.getUserCode();
    10.                 check = 0;
    11.             }
    12.             else
    13.             {
    14.                 check += 1;
    15.             }
    16.         }
    17.         if (check == 0)
    18.         {
    19.             return true;
    20.         }
    21.         else
    22.         {
    23.             return false;
    24.              Debug.Log(check);
    25.         }
    26.      
    27.     }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    There's a bug in your validation method: if you type a valid code, all the correct data is set but the loop continues, so if there's an invalid code that follows then check is increased and the method returns false. Instead, return from the method from within the success check:
    Code (csharp):
    1.  
    2. bool IsValid(string[] codeList, string pin)
    3. {
    4.    foreach (string code in usersPincodes)
    5.    {
    6.        if(code == pin)
    7.        {
    8.            dataTracking.instance.userCode = patients[code];
    9.            welcomeText.text = "Welcome " + dataTracking.instance.getUserCode();
    10.  
    11.            // exit early because we found a match
    12.            return true;
    13.        }
    14.    }
    15.  
    16.    // if we're still here, the pin must be incorrect, so return false
    17.    return false;
    18. }
    19.  
    You could also use Array.IndexOf instead of a foreach loop.
     
    LuryannC likes this.