Search Unity

Resolved Why Doesn't It Works

Discussion in 'Scripting' started by omerfarukkelkitli, Mar 5, 2023.

  1. omerfarukkelkitli

    omerfarukkelkitli

    Joined:
    Sep 4, 2021
    Posts:
    2
    Could you please take a look at this script? I think i did everything right but i can't take result...

    Code (CSharp):
    1. public void ButtonCheck()
    2.     {
    3.        
    4.         if(Name==null || Password==null || PassAgain == null)
    5.         {
    6.             Wrong.SetActive(true);
    7.         }
    8.  
    9.         if(Name!=null && Password!=null && PassAgain != null)
    10.         {
    11.             if (Password == PassAgain)
    12.             {
    13.                 SceneManager.LoadScene("LoginScreen");
    14.             }
    15.             if (Password != PassAgain)
    16.             {
    17.                 Wrong.SetActive(true);
    18.             }
    19.         }
    Even if i don't take any errors in Visual Studio it doesn't work...
    (Also added this script to a empty GameObject and used OnClick.
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    You're missing a closing bracket for the function. You're also only checking if the strings are null but not if they're empty. If they're all empty they will compare and be the same. Null and empty are not the same thing when it comes to a string. You could also condense that down a bit.

    Code (CSharp):
    1. public string fullName;
    2. public string password;
    3. public string passAgain;
    4. public GameObject wrong;
    5.  
    6. public void ButtonCheck () {
    7.      
    8.     if (string.IsNullOrEmpty (fullName)) wrong.SetActive (true);
    9.     else if (string.IsNullOrEmpty (password)) wrong.SetActive (true);
    10.     else if (string.IsNullOrEmpty (passAgain)) wrong.SetActive (true);
    11.     else if (password == passAgain) print ("Correct");
    12.     else if (password != passAgain) wrong.SetActive (true);
    13.  
    14. }
    https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorempty?view=net-7.0
     
  3. omerfarukkelkitli

    omerfarukkelkitli

    Joined:
    Sep 4, 2021
    Posts:
    2
    It worked flawlessly, thank for your help bro <3<3<3
     
    QuinnWinters likes this.