Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by Skyfall106, Jan 14, 2019.

  1. Skyfall106

    Skyfall106

    Joined:
    Mar 5, 2017
    Posts:
    132
    Hey! So im tryingt o make a kind of save system in my game, but its chucking me an error which is really annoying. It says there is an error at line 123 but I have no clue whats wrong, if somebody could help out i would be really grateful.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class FieldController : MonoBehaviour
    7. {
    8.  
    9.     bool discardChanges = false;
    10.  
    11.     public GameObject quitDisclaimer;
    12.     public GameObject savesDisclaimer;
    13.  
    14.     public GameObject gameTitle;
    15.     public GameObject developer;
    16.  
    17.     public string saves = "0";
    18.  
    19.     public InputField planNameInput;
    20.     public InputField developerNameInput;
    21.  
    22.     public Button save1;
    23.     public Button save2;
    24.     public Button save3;
    25.     public Button save4;
    26.     public Button save5;
    27.     public Button save6;
    28.  
    29.     void OnEnable()
    30.     {
    31.  
    32.         if (PlayerPrefs.GetString("save") == "1")
    33.         {
    34.             gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("1");
    35.             developer.GetComponent<Text>().text = PlayerPrefs.GetString("11");
    36.         }
    37.  
    38.         if (PlayerPrefs.GetString("save") == "2")
    39.         {
    40.             gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("2");
    41.             developer.GetComponent<Text>().text = PlayerPrefs.GetString("22");
    42.         }
    43.         if (PlayerPrefs.GetString("save") == "3")
    44.         {
    45.             gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("3");
    46.             developer.GetComponent<Text>().text = PlayerPrefs.GetString("33");
    47.         }
    48.         if (PlayerPrefs.GetString("save") == "4")
    49.         {
    50.             gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("4");
    51.             developer.GetComponent<Text>().text = PlayerPrefs.GetString("44");
    52.         }
    53.         if (PlayerPrefs.GetString("save") == "5")
    54.         {
    55.             gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("5");
    56.             developer.GetComponent<Text>().text = PlayerPrefs.GetString("55");
    57.         }
    58.         if (PlayerPrefs.GetString("save") == "6")
    59.         {
    60.             gameTitle.GetComponent<Text>().text = PlayerPrefs.GetString("6");
    61.             developer.GetComponent<Text>().text = PlayerPrefs.GetString("66");
    62.         }
    63.  
    64.         LoadInputFields();
    65.     }
    66.  
    67.     void OnApplicationQuit()
    68.     {
    69.         if (!discardChanges)
    70.         {
    71.             SaveInputFields();
    72.         }
    73.     }
    74.  
    75.     public void OpenQuitDisclaimer()
    76.     {
    77.         quitDisclaimer.SetActive(true);
    78.     }
    79.  
    80.     public void CancelQuitDisclaimer()
    81.     {
    82.         quitDisclaimer.SetActive(false);
    83.     }
    84.     public void OpenSavesDisclaimer()
    85.     {
    86.         savesDisclaimer.SetActive(true);
    87.     }
    88.  
    89.     public void CloseSavesDisclaimer()
    90.     {
    91.         savesDisclaimer.SetActive(false);
    92.     }
    93.  
    94.     public void QuitWithoutSaving()
    95.     {
    96.         discardChanges = true;
    97.         Application.Quit();
    98.     }
    99.  
    100.     void SaveInputFields()
    101.     {
    102.         var foundInputFields = FindObjectsOfType<InputField>();
    103.         foreach (var inputField in foundInputFields)
    104.         {
    105.             var parentName = inputField.transform.parent.name;
    106.             PlayerPrefs.SetString(parentName + inputField.name, inputField.text);
    107.         }
    108.     }
    109.  
    110.     void LoadInputFields()
    111.     {
    112.         var foundInputFields = FindObjectsOfType<InputField>();
    113.         foreach (var inputField in foundInputFields)
    114.         {
    115.             var parentName = inputField.transform.parent.name;
    116.             inputField.text = PlayerPrefs.GetString(parentName + inputField.name);
    117.         }
    118.     }
    119.  
    120.     public void CreatePlan()
    121.     {
    122.        
    123.         string planName = planNameInput.GetComponentInChildren<Text>().text;
    124.         string developerName = developerNameInput.GetComponentInChildren<Text>().text;
    125.  
    126.         string save = "";
    127.  
    128.         if (saves == "0")
    129.         {
    130.             save = "1";
    131.             PlayerPrefs.SetString("1", planName);
    132.             PlayerPrefs.SetString("11", developerName);
    133.             PlayerPrefs.SetString("save", save);
    134.             Application.LoadLevel("Main");
    135.         }
    136.         else if (saves == "1")
    137.         {
    138.             save = "2";
    139.             PlayerPrefs.SetString("2", planName);
    140.             PlayerPrefs.SetString("22", developerName);
    141.             PlayerPrefs.SetString("save", save);
    142.             Application.LoadLevel("Main");
    143.         }
    144.         else if (saves == "2")
    145.         {
    146.             save = "3";
    147.             PlayerPrefs.SetString("3", planName);
    148.             PlayerPrefs.SetString("33", developerName);
    149.             PlayerPrefs.SetString("save", save);
    150.             Application.LoadLevel("Main");
    151.         }
    152.         else if (saves == "3")
    153.         {
    154.             save = "4";
    155.             PlayerPrefs.SetString("4", planName);
    156.             PlayerPrefs.SetString("44", developerName);
    157.             PlayerPrefs.SetString("save", save);
    158.             Application.LoadLevel("Main");
    159.         }
    160.         else if (saves == "4")
    161.         {
    162.             save = "5";
    163.             PlayerPrefs.SetString("5", planName);
    164.             PlayerPrefs.SetString("55", developerName);
    165.             PlayerPrefs.SetString("save", save);
    166.             Application.LoadLevel("Main");
    167.         }
    168.         else if (saves == "5")
    169.         {
    170.             save = "6";
    171.             PlayerPrefs.SetString("6", planName);
    172.             PlayerPrefs.SetString("66", developerName);
    173.             PlayerPrefs.SetString("save", save);
    174.             Application.LoadLevel("Main");
    175.         }
    176.  
    177.         if (saves == "6")
    178.         {
    179.             OpenSavesDisclaimer();
    180.         }
    181.  
    182.     }
    183.  
    184.     public void Saves() {
    185.  
    186.         if (PlayerPrefs.GetString("1") != "")
    187.         {
    188.             string text = PlayerPrefs.GetString("1");
    189.             save1.GetComponentInChildren<Text>().text = text;
    190.         }
    191.  
    192.         if (PlayerPrefs.GetString("2") != "")
    193.         {
    194.             string text = PlayerPrefs.GetString("2");
    195.             save2.GetComponentInChildren<Text>().text = text;
    196.         }
    197.         if (PlayerPrefs.GetString("3") != "")
    198.         {
    199.             string text = PlayerPrefs.GetString("3");
    200.             save3.GetComponentInChildren<Text>().text = text;
    201.         }
    202.         if (PlayerPrefs.GetString("4") != "")
    203.         {
    204.             string text = PlayerPrefs.GetString("4");
    205.             save4.GetComponentInChildren<Text>().text = text;
    206.         }
    207.         if (PlayerPrefs.GetString("5") != "")
    208.         {
    209.             string text = PlayerPrefs.GetString("5");
    210.             save5.GetComponentInChildren<Text>().text = text;
    211.         }
    212.         if (PlayerPrefs.GetString("6") != "")
    213.         {
    214.             string text = PlayerPrefs.GetString("6");
    215.             save6.GetComponentInChildren<Text>().text = text;
    216.         }
    217.  
    218.     }
    219.  
    220.     public void Save1()
    221.     {
    222.         var save = PlayerPrefs.GetString("save");
    223.         save = "1";
    224.         PlayerPrefs.SetString("save", save);
    225.         Application.LoadLevel("Main");
    226.     }
    227.  
    228.     public void Save2()
    229.     {
    230.         var save = PlayerPrefs.GetString("save");
    231.         save = "2";
    232.         PlayerPrefs.SetString("save", save);
    233.         Application.LoadLevel("Main");
    234.     }
    235.  
    236.     public void Save3()
    237.     {
    238.         var save = PlayerPrefs.GetString("save");
    239.         save = "3";
    240.         PlayerPrefs.SetString("save", save);
    241.         Application.LoadLevel("Main");
    242.     }
    243.  
    244.     public void Save4()
    245.     {
    246.         var save = PlayerPrefs.GetString("save");
    247.         save = "4";
    248.         PlayerPrefs.SetString("save", save);
    249.         Application.LoadLevel("Main");
    250.     }
    251.  
    252.     public void Save5()
    253.     {
    254.         var save = PlayerPrefs.GetString("save");
    255.         save = "5";
    256.         PlayerPrefs.SetString("save", save);
    257.         Application.LoadLevel("Main");
    258.     }
    259.  
    260.     public void Save6()
    261.     {
    262.         var save = PlayerPrefs.GetString("save");
    263.         save = "6";
    264.         PlayerPrefs.SetString("save", save);
    265.         Application.LoadLevel("Main");
    266.     }
    267.  
    268. }
    269.  
     
  2. Have you dragged the proper input GO in the Plan Name Input field in the inspector?
     
  3. Skyfall106

    Skyfall106

    Joined:
    Mar 5, 2017
    Posts:
    132
    Yeah i have
     
  4. Do you have a Text component on any of its children?
    Could you Debug Log out both the planNameInput and the planNameInput.GetComponentInChildren<Text>() right before the 123rd line? So we can see what is null?