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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

(Help) Scripting Error: NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by saberwolfcdw, Oct 9, 2015.

  1. saberwolfcdw

    saberwolfcdw

    Joined:
    Sep 21, 2015
    Posts:
    14
    I am in the process of swapping the game text just outputing to the console to text showing in a unity text box so not all the scripting is complete. I was just testing what I have done. I am very new to coding so I apologize in advance if it is something really simple I messed up. I am betting it is.
    Unity is giving me this error.

    NullReferenceException: Object reference not set to an instance of an object
    NumberWizard.startgame () (at Assets/NumberWizard.cs:36)
    NumberWizard.Update () (at Assets/NumberWizard.cs:23)

    When I try to run this script. I dont see the issue on line 36 and 23

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class NumberWizard : MonoBehaviour {

    // Use this for initialization
    public Text text;
    public enum States {startgame, firstguess, nextguess, win}
    public States myState;
    int max;
    int min;
    int guess;


    void Start () {
    myState = States.startgame;

    }

    // Update is called once per frame
    void Update () {
    23 if (myState == States.startgame) {startgame();}
    else if (myState == States.firstguess) {firstguess();}
    else if (myState == States.nextguess) {nextguess();}
    else if (myState == States.win) {win();}

    }

    void startgame() {

    min = 1;
    max = 1000;
    guess = Random.Range(min,max);

    36 text.text = "=======================================================================\n" +
    "Welcome to Number Wizard\n\n" +
    "Pick a number in your head, but don't tell me.\n\n" +

    "Number must be between " + min +
    "and " + max +
    ".\n\n" +

    "Press Enter when you have your number selected.";
    max = max + 1;
    if (Input.GetKeyDown(KeyCode.Return)) {myState = States.firstguess;}

    }



    void firstguess () {

    print ("Is the number " + guess);
    print ("Type Up Arrow for higher, Down Arrow for lower, and Enter for equals.");
    if (Input.GetKeyDown(KeyCode.UpArrow)) {
    min = guess;
    myState = States.nextguess;
    }
    else if (Input.GetKeyDown(KeyCode.DownArrow)) {
    max = guess;
    myState = States.nextguess;
    }
    else if (Input.GetKeyDown(KeyCode.Return)) {
    myState = States.win;
    }

    }


    void nextguess () {

    guess = (max + min) / 2;
    print ("Is the number " + guess);
    print ("Type Up Arrow for higher, Down Arrow for lower, and Enter for equals.");
    if (Input.GetKeyDown(KeyCode.UpArrow)) {
    min = guess;
    myState = States.nextguess;
    }
    else if (Input.GetKeyDown(KeyCode.DownArrow)) {
    max = guess;
    myState = States.nextguess;
    }
    else if (Input.GetKeyDown(KeyCode.Return)) {
    myState = States.win;
    }

    }

    void win (){

    print ("Your number was " + guess);
    print ("I win!!");
    }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Use [code ] code tags[/code] - it will let us see line numbers.

    As for your problem, your text reference must be null. Make sure you have assigned it in the Inspector.
     
  3. saberwolfcdw

    saberwolfcdw

    Joined:
    Sep 21, 2015
    Posts:
    14
    Ok so I am an idiot. I had to assign the text box to the text the script was out putting.
     
  4. saberwolfcdw

    saberwolfcdw

    Joined:
    Sep 21, 2015
    Posts:
    14
    That was it. thanks. I knew it was something stupid
     
  5. saberwolfcdw

    saberwolfcdw

    Joined:
    Sep 21, 2015
    Posts:
    14
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class NumberWizard : MonoBehaviour {
    7.  
    8. // Use this for initialization
    9. public Text text;
    10. public enum States {startgame, firstguess, nextguess, win}
    11. public States myState;
    12. int max;
    13. int min;
    14. int guess;
    15.  
    16.  
    17. void Start () {
    18. myState = States.startgame;
    19.  
    20. }
    21.  
    22. // Update is called once per frame
    23. void Update () {
    24. if (myState == States.startgame) {startgame();}
    25. else if (myState == States.firstguess) {firstguess();}
    26. else if (myState == States.nextguess) {nextguess();}
    27. else if (myState == States.win) {win();}
    28.  
    29. }
    30.  
    31. void startgame() {
    32.  
    33. min = 1;
    34. max = 1000;
    35. guess = Random.Range(min,max);
    36.  
    37. text.text = "=======================================================================\n" +
    38. "Welcome to Number Wizard\n\n" +
    39. "Pick a number in your head, but don't tell me.\n\n" +
    40.  
    41. "Number must be between " + min +
    42. "and " + max +
    43. ".\n\n" +
    44.  
    45. "Press Enter when you have your number selected.";
    46. max = max + 1;
    47. if (Input.GetKeyDown(KeyCode.Return)) {myState = States.firstguess;}
    48.  
    49. }
    50.  
    51.  
    52.  
    53. void firstguess () {
    54.  
    55. print ("Is the number " + guess);
    56. print ("Type Up Arrow for higher, Down Arrow for lower, and Enter for equals.");
    57. if (Input.GetKeyDown(KeyCode.UpArrow)) {
    58. min = guess;
    59. myState = States.nextguess;
    60. }
    61. else if (Input.GetKeyDown(KeyCode.DownArrow)) {
    62. max = guess;
    63. myState = States.nextguess;
    64. }
    65. else if (Input.GetKeyDown(KeyCode.Return)) {
    66. myState = States.win;
    67. }
    68.  
    69. }
    70.  
    71.  
    72. void nextguess () {
    73.  
    74. guess = (max + min) / 2;
    75. print ("Is the number " + guess);
    76. print ("Type Up Arrow for higher, Down Arrow for lower, and Enter for equals.");
    77. if (Input.GetKeyDown(KeyCode.UpArrow)) {
    78. min = guess;
    79. myState = States.nextguess;
    80. }
    81. else if (Input.GetKeyDown(KeyCode.DownArrow)) {
    82. max = guess;
    83. myState = States.nextguess;
    84. }
    85. else if (Input.GetKeyDown(KeyCode.Return)) {
    86. myState = States.win;
    87. }
    88.  
    89. }
    90.  
    91. void win (){
    92.  
    93. print ("Your number was " + guess);
    94. print ("I win!!");
    95. }
    96. }
    97.