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
  4. Dismiss Notice

Object reference not set to an instance of an object error

Discussion in 'Editor & General Support' started by kentg1, May 3, 2017.

  1. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    167
    UNity Novice here.

    I have a working game to add two numbers and then show the answer on screen, It works just fine with this script.


    [script]
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class NumberLine : MonoBehaviour {

    public InputField number1;
    public InputField number2;
    public Slider numberLine;
    public Text coordinate;
    public GameObject spawner;
    public Canvas c;

    //public Button calc;
    private int result = 0;
    private int n1 = 0;
    private int n2 = 0;
    public Text results;
    // Use this for initialization


    // Update is called once per frame
    void Update () {
    result = n1 + n2;
    //string r = result.ToString();
    Debug.Log(result.ToString());
    results.text = result.ToString();
    }

    public void Calculate() {

    n1 = int.Parse(number1.text);
    n2 = int.Parse(number2.text);
    result = n1 + n2;
    numberLine.value = result;
    Text coord = Instantiate(coordinate, coordinate.transform.position, Quaternion.identity) as Text;
    coord.transform.parent = c.transform;
    coord.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
    coord.text = result.ToString();

    }
    }
    [/script]


    So I want to add a 3rd number to be added for the game. When I play the game I can enter the 3rd number on the canvas but when I hit enter it does not calculate at all, instead it goes back the scene screen with this error message?


    NullReferenceException: Object reference not set to an instance of an object
    NumberLine.Calculate () (at Assets/NumberLine.cs:36) ( I have it in blue below)

    [script}]
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class NumberLine : MonoBehaviour {

    public InputField number1;
    public InputField number2;
    public InputField number3;
    public Slider numberLine;
    public Text coordinate;
    public GameObject spawner;
    public Canvas c;

    //public Button calc;
    private int result = 0;
    private int n1 = 0;
    private int n2 = 0;
    private int n3 = 0;
    public Text results;
    // Use this for initialization


    // Update is called once per frame
    void Update () {
    result = n1 + n2 + n3;
    //string r = result.ToString();
    Debug.Log(result.ToString());
    results.text = result.ToString();
    }

    public void Calculate() {

    n1 = int.Parse(number1.text);
    n2 = int.Parse(number2.text);
    n3 = int.Parse(number3.text);
    result = n1 + n2 + n3;
    numberLine.value = result;
    Text coord = Instantiate(coordinate, coordinate.transform.position, Quaternion.identity) as Text;
    coord.transform.parent = c.transform;
    coord.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
    coord.text = result.ToString();

    }
    }
    [/script]

    Any ideas?

    Thanks
     

    Attached Files:

  2. RPKMN1

    RPKMN1

    Joined:
    Feb 13, 2017
    Posts:
    34
    if the blue line really is line 36, the only option is that number3 is not assigned. if you have it as a public field i'm guessing you assign it in the editor. double check that it's set there.
     
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
  4. kentg1

    kentg1

    Joined:
    Dec 22, 2014
    Posts:
    167
    Ah you are both right I did not assign text three in the inspector for my public variable

    Thanks!
     
  5. RPKMN1

    RPKMN1

    Joined:
    Feb 13, 2017
    Posts:
    34
    glad you got it sorted out :)