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

Keep getting NullRefrenceException. I'm fairly new to coding and I don't quite understand.pls help

Discussion in 'Scripting' started by tyhuisk, Dec 4, 2020.

  1. tyhuisk

    tyhuisk

    Joined:
    Nov 13, 2020
    Posts:
    1
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. using DatabaseControl; // << Remember to add this reference to your scripts which use DatabaseControl
    6.  
    7. public class LoginMenu : MonoBehaviour
    8. {
    9.     //these variable are set in the inspector:
    10.  
    11.     // they are enabled and disabled to show and hide different parts of the UI
    12.     public GameObject login_object;
    13.     public GameObject register_object;
    14.     public GameObject loading_object;
    15.  
    16.     //these are the login input feilds:
    17.     public InputField input_login_username;
    18.     public InputField input_login_password;
    19.  
    20.     //register input fields:
    21.     public InputField input_register_username;
    22.     public InputField input_register_password;
    23.     public InputField input_register_confirmPassword;
    24.  
    25.     //red error text
    26.     public Text login_error;
    27.     public Text register_error;
    28.  
    29.     ////these variables cannot be set in the inspector:
    30.  
    31.     //the part of UI currently being shown
    32.     // 0 = login, 1 = register, 2 = logged in, 3 = loading
    33.     int part = 0;
    34.     //scene starts showing login
    35.  
    36.  
    37.  
    38.     bool isDatabaseSetup = true;
    39.    
    40.  
    41.     private void Start()
    42.     {
    43.         // this checks wether database is setup. it is used to prevent errors for users who try to
    44.         //without having setup a database.
    45.         //you dont need to use this bool as it will work without it as long as the database has been setup
    46.         TextAsset dataFile = Resources.Load("data") as TextAsset;
    47.         string[] splitdatafile = dataFile.text.Split(new string[] { "-" }, StringSplitOptions.None);
    48.         if (splitdatafile[0] == "0")
    49.         {
    50.             isDatabaseSetup = false;
    51.             Debug.Log("These demos will not work out of the box.");
    52.         }
    53.         else
    54.         {
    55.             isDatabaseSetup = true;
    56.         }
    57.  
    58.         //sets error Texts string to blank
    59.         blankErrors();
    60.  
    61.     }
    62.  
    63.     private void Update()
    64.     {
    65.         if (isDatabaseSetup == true)
    66.         {
    67.             //enables and disables the different objects to show correct part
    68.             if (part == 0)
    69.             {
    70.                 login_object.gameObject.SetActive(true);
    71.                 register_object.gameObject.SetActive(false);
    72.                 loading_object.gameObject.SetActive(false);
    73.             }
    74.             if (part == 1)
    75.             {
    76.                 login_object.gameObject.SetActive(false);
    77.                 register_object.gameObject.SetActive(true);
    78.                 loading_object.gameObject.SetActive(false);
    79.             }
    80.             if (part == 2)
    81.             {
    82.                 //We are logged in - We have transitioned to a new scene
    83.             }
    84.             if (part == 3)
    85.             {
    86.                 login_object.gameObject.SetActive(false);
    87.                 register_object.gameObject.SetActive(false);
    88.                 loading_object.gameObject.SetActive(true);
    89.             }
    90.         }
    91.     }
    92.      void blankErrors()
    93.     {
    94.         //blank errors texts when part is changed e.g login > Register
    95.         login_error.text = "";
    96.         register_error.text = "";
    97.     }
    98.  
    99.     public void login_Register_Button ()
    100.     {
    101.         part = 1; //show register UI
    102.         blankErrors();
    103.     }
    104.  
    105.     public void register_Back_Btton ()
    106.     {
    107.         part = 0; //goes back to showing login UI
    108.         blankErrors();
    109.     }
    110.  
    111.     public void data_LogOut_Button()
    112.     {
    113.         part = 0;//goes back to showing login UI
    114.  
    115.         UserAccountManager.instance.LogOut();
    116.  
    117.         blankErrors();
    118.     }
    119.  
    120.     public void login_login_Button ()
    121.     {
    122.         if (isDatabaseSetup == true)
    123.         {
    124.             if ((input_login_password.text != "") && (input_login_password.text != ""))
    125.             {
    126.                 if ((input_login_username.text.Contains ("-")) || (input_login_password.text.Contains ("-")))
    127.                 {
    128.                     login_error.text = "unsupported Symbol '-'";
    129.                     input_login_password.text = "";
    130.                 } else
    131.                 {
    132.                     StartCoroutine(sendLoginRequest(input_login_username.text, input_login_password.text));
    133.                     part = 3;                  
    134.                 }
    135.             } else
    136.             {
    137.                 login_error.text = "Field Blank!";
    138.                 input_login_password.text = "";
    139.             }
    140.         }
    141.     }
    142.  
    143.     IEnumerator sendLoginRequest (string username, string password)
    144.     {
    145.         if (isDatabaseSetup == true)
    146.         {
    147.             IEnumerator e = DCF.Login(username, password);
    148.             while (e.MoveNext())
    149.             {
    150.                 yield return e.Current;
    151.             }
    152.  
    153.             WWW returned = e.Current as WWW;
    154.             if (returned.text == "success")
    155.             {
    156.                 //password was correct
    157.                 blankErrors();
    158.                 part = 2;//show logged in UI
    159.  
    160.                 //blank username field
    161.                 input_login_username.text = "";
    162.  
    163.                 UserAccountManager.instance.LogIn(username, password);
    164.             }
    165.             if (returned.text == "incorrectUser")
    166.             {
    167.                 //Account with username not found in database
    168.                 login_error.text = "Username not found";
    169.                 part = 0;
    170.             }
    171.             if (returned.text == "incorrectPass")
    172.             {
    173.                 //Account with username found, but password wrong
    174.                 part = 0;
    175.                 login_error.text = "Incorrect Password";
    176.                
    177.             }
    178.             if (returned.text == "ContainsUnsupportedSymbol")
    179.             {
    180.                 //one of the parameters contained a - symbol
    181.                 part = 0;
    182.                 login_error.text = "Unsupported Symbol '-'";
    183.  
    184.             }
    185.             if (returned.text == "Error")
    186.             {
    187.                 //Account not Created
    188.                 part = 0;
    189.                 login_error.text = "Database Error. Try again later.";
    190.  
    191.             }
    192.  
    193.             input_login_password.text = "";
    194.         }
    195.     }
    196.  
    197.     public void register_register_Button ()
    198.     {
    199.         if (isDatabaseSetup == true)
    200.         {
    201.             if ((input_register_username.text != "") && (input_register_password.text != "") && (input_register_confirmPassword.text != ""))
    202.             {
    203.                 if (input_register_username.text.Length > 4)
    204.                 {
    205.                     if (input_register_password.text.Length > 6)
    206.                     {
    207.                         if (input_register_password.text == input_register_confirmPassword.text)
    208.                         {
    209.                             if ((input_login_username.text.Contains ("-")) || (input_login_password.text.Contains ("-")))
    210.                             {
    211.                                 register_error.text = "Unsupported Symbol '-'";
    212.                                 input_register_password.text = "";
    213.                                 input_register_confirmPassword.text = "";
    214.                             } else
    215.                             {
    216.                                 StartCoroutine(sendRegisterRequest(input_register_username.text, input_register_password.text, input_register_confirmPassword.text));
    217.                                 part = 3;
    218.                             }
    219.  
    220.                         } else
    221.                         {
    222.                             register_error.text = "Passwords don't match!";
    223.                             input_register_password.text = "";
    224.                             input_register_confirmPassword.text = "";
    225.                         }
    226.  
    227.                     }
    228.                     else
    229.                     {
    230.                         register_error.text = "Passwords too short";
    231.                         input_register_password.text = "";
    232.                         input_register_confirmPassword.text = "";
    233.                     }
    234.  
    235.                 }
    236.                 else
    237.                 {
    238.                     register_error.text = "Username too short";
    239.                     input_register_password.text = "";
    240.                     input_register_confirmPassword.text = "";
    241.                 }
    242.  
    243.             }
    244.             else
    245.             {
    246.                 register_error.text = "Field Blank!";
    247.                 input_register_password.text = "";
    248.                 input_register_confirmPassword.text = "";
    249.             }
    250.         }
    251.     }
    252.  
    253.     IEnumerator sendRegisterRequest (string username, string password, string data)
    254.     {
    255.         if (isDatabaseSetup == true)
    256.         {
    257.             IEnumerator ee = DCF.RegisterUser(username, password, data);
    258.             while(ee.MoveNext())
    259.             {
    260.                 yield return ee.Current;
    261.             }
    262.             WWW returnedd = ee.Current as WWW;
    263.  
    264.             if (returnedd.text == "Success")
    265.             {
    266.                 blankErrors();
    267.                 part = 2;
    268.  
    269.                 input_register_username.text = "";
    270.  
    271.                 UserAccountManager.instance.LogIn(username, password);
    272.             }
    273.             if (returnedd.text == "usernameInUse")
    274.             {
    275.                 part = 1;
    276.                 register_error.text = "Username Unavailable. Try another.";
    277.             }
    278.             if (returnedd.text == "ContainsUnsupportedSymbol")
    279.             {
    280.                 part = 1;
    281.                 register_error.text = "Unsupported Symbol '-'";
    282.             }
    283.             if (returnedd.text == "Error")
    284.             {
    285.                 part = 1;
    286.                 register_error.text = "Database Error. Try again later.";
    287.             }
    288.  
    289.             input_register_password.text = "";
    290.             input_register_confirmPassword.text = "";
    291.         }
    292.     }
    293.  
    294. }
    295.  
     
  2. thesupersoup

    thesupersoup

    Joined:
    Nov 27, 2017
    Posts:
    70
    That's a whole lot of code. We need to focus in on where the issue is arising.
    Can you copy/paste the NullReferenceException error here?
     
  3. AbandonedCrypt

    AbandonedCrypt

    Joined:
    Apr 13, 2019
    Posts:
    69
    I‘m just assuming your dataFile doesn‘t load properly from Resources, since you don‘t check if it‘s null before accessing it.