Creating user input with buttons

I am really new to coding and game development. I am creating a scene containing 10 buttons representing numbers 0 through 9 and 1 input field. I have created the attached scripts for the buttons representing numbers to allow users to enter a string of numbers into the input field using those buttons. I have used both of the following scripts by placing them in a Manager object within the scene and then attaching the script to each button as a component. I then drag the input field into the inspector. When I run the scene each button click results in the number being placed in the input field as desired. However, I continually get an error on the line including the AddListener. The error states “NullReferenceException: Object reference not set to an instance of an object”. Then is goes on to identify that specific line. Can anyone offer insight into why I am getting this error?

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

public class NumberClick : MonoBehaviour {

    Button button;
    public string number;
    public InputField numInput;

    void Start() {
        button = this.GetComponent<Button>();
        button.onClick.AddListener(delegate { TaskOnClick(); });
    }   
    public void TaskOnClick()
    {
        numInput.text += number;
    }
    void Destroy()
    {
        button.onClick.RemoveListener(TaskOnClick);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NumberClick : MonoBehaviour {

    Button button;
    public string number;
    public InputField numInput;

    void Start() {
        button = this.GetComponent<Button>();
        button.onClick.AddListener(TaskOnClick);
    }   
    public void TaskOnClick()
    {
        numInput.text += number;
    }
    void Destroy()
    {
        button.onClick.RemoveListener(TaskOnClick);
    }
}

I have watched multiple videos and read other forums on similar problems. I have made many changes to see if the issue will be corrected, but cannot find one that fixes the problem. Several of the changes moved the reference to AddListener to a different line and the error still showed on the original line. I’m confused.

You don’t want these 2 scripts on the Manager, I’d say.-- just on the buttons.
That’s probably what’s giving you the error. And you (probably?) pasted in the wrong (2nd) script, because these 2 are identical here.
If there’s a different one, you could post that, but for sure remove this from the manager. :slight_smile:

Wow, you’re working much harder than you need to here. You should almost never need to call AddListener and RemoveListener. If you’re doing that, you’re either doing it wrong, or trying something quite advanced.

Instead, you only need a script like this:

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(InputField))
public class InputFieldActions : MonoBehaviour {

    InputField inputField;
   
    void Awake() {
        inputField = GetComponent<InputField>();
    }
   
    public void Append(string textToAdd) {
        inputField.text += textToAdd;
    }
}

Put this InputFieldActions on the InputField. Then, use the Inspector of each button to call this Append method on the field when the button is clicked, passing in whatever number you want to add. That’s it. You don’t need any code on the buttons at all, and you certainly don’t need to be mucking about with Add/RemoveListener.

Now suppose you want another button that clears the input field. Simply add another public method in InputFieldActions, that clears the text. Invoke this method from whatever button you want to do that. Again, job done.

UnityEvents are really great, and not just for user input — check out my 15-minute tutorial on how to use them properly.

Thank you for this. It was all it was. I was taking something from other code notes that shouldn’t have transferred over. Thank you again. Quick fix.

JoeStrout - Thank you so much for the code. I am researching it now to understand the syntax and terms. I hope to implement this in my project. I really appreciate it.

1 Like

np you’re welcome.

Oh, and it’s good to know/learn the stuff that Joe wrote, too, for sure.

Anyways, glad ya got it working. :wink: