Unity UI button not working?

I have this code that is supposed to trigger when a UI button is pressed, but it keeps saying
“NullReferenceException: Object reference not set to an instance of an object
SpawnManager.Update () (at Assets/Scripts/SpawnManager.cs:32)”

However, all objects and such should be referenced in the editor so im kinda scratching my head here. Can someone help me out?

here is the relevent code.

public Button waveStart;
public GameObject uiCanvas;

void Start()
    {
        waveStart = uiCanvas.GetComponent<Button>();
    {
void Update()
    {
        waveStart.onClick.AddListener(onPress);
    }
void onPress()
    {
        startWave = true;
    }

Your script isn’t complete, What is line 32?

By the name of “uiCanvas” variable I believe you’re assigning your canvas’ gameobject instead of button’s gameobject in your script.

Actually you don’t need button’s gameObject and only its “Button” Component is enough for adding onClick Listener, it’s better to call AddListener() method once in Start() method.

I don’t know what are you trying to do but a simple script to read button click is something like:

using UnityEngine;
using UnityEngine.UI;

public class SpawnManager : MonoBehaviour
{
    // assign reference in inspector
    public Button WaveStart;

    private void Start()
    {
        // adding listener
        WaveStart.onClick.AddListener(OnPress);
    }

    private void OnPress()
    {
        // do on press functionality here
    }
}

I hope I have helped.