Missing Spin Button and null reference exception

NullReferenceException: Object reference not set to an instance of an object
ActionButtonUI.SetBaseAction (BaseAction baseAction) (at Assets/Scripts/ActionButtonUI.cs:15)
UnitActionSystemUI.CreateUnitActionButtons () (at Assets/Scripts/UnitActionSystemUI.cs:30)
UnitActionSystemUI.Start () (at Assets/Scripts/UnitActionSystemUI.cs:15)

I’m getting a NRE from the anonymous function we set up in the last lecture but the error only popped up after the refactoring of the code in this lecture. I went through and triple checked my code against the lecture and even copied and pasted all of the scripts we’ve changed in the last 2 lectures to still get this error.

While trying to debug, the actions are in the container but when being instantiated they aren’t being created. I added debug logs to show which action was being created but the log doesn’t show in the console.

foreach (BaseAction baseAction in selectedUnit.GetBaseActionArray()){
            Transform actionButtonTransform = Instantiate(actionButtonPrefab, actionButtonContainerTransform);
            ActionButtonUI actionButtonUI = actionButtonTransform.GetComponent<ActionButtonUI>();
            actionButtonUI.SetBaseAction(baseAction);
            Debug.Log("BaseAction " + baseAction);
        }

The NRE error is the only error I’m getting

1 Like

What does your GetBaseActionArray() look like? It looks like it’s returning something, but that something is null.

public BaseAction[] GetBaseActionArray() {
        return baseActionArray;
    }

I threw a debug log in to display the length and contents and it shows both actions
actions

Check if the ActionButtonUI is not null. There is a chance that it could be

foreach (BaseAction baseAction in selectedUnit.GetBaseActionArray())
{
    Transform actionButtonTransform = Instantiate(actionButtonPrefab, actionButtonContainerTransform);
    ActionButtonUI actionButtonUI = actionButtonTransform.GetComponent<ActionButtonUI>();
    Debug.Log($"ActionbuttonUI {actionButtonUI}");
    actionButtonUI.SetBaseAction(baseAction);
    Debug.Log("BaseAction " + baseAction);
}
ActionbuttonUI ActionButtonUI(Clone) (ActionButtonUI)

ActionButtonUI isn’t null, I get the button for move, just not spin.

Looking at the NRE again, I can see that the error comes from ActionButtonUI line 15. Check what is on that line that could be null. I looked at the repository and line 15 there does not look like it is the one in question.

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

public class ActionButtonUI : MonoBehaviour{

    [SerializeField] private TextMeshProUGUI textMeshPro;
    [SerializeField] private Button button;

    public void SetBaseAction(BaseAction baseAction){
        textMeshPro.text = baseAction.GetActionName().ToUpper();

        **button.onClick.AddListener(() => {**
            UnitActionSystem.Instance.SetSelectedAction(baseAction);
        });
    }

}

For me line 15 is the start of the anonymous function we set up

OK, so there’re only two things that can be null here; button or UnitActionSystem.Instance. Check that you have assigned a button in the inspector for all the instances.

Screenshot 2023-03-15 170811

image

Prior to reformatting the code, both buttons worked and displayed just fine. I went back and copied the code from this lecture and I still don’t understand how it doesn’t work all of a sudden

The field is on the ActionButtonUI component, not the UnitActionSystemUI component. I can see it’s a prefab. Can you show the prefab?

image

Can you show the full code for CreateUnitActionButtons? I can replicate your error, but only based on a suspicion.

image


Edit
Actually, I don’t think the code makes a difference. But I got it to error like that with a series of unfortunate events. When we set up the container, there are buttons in the container at design time. Those buttons get deleted when the actual buttons get created, but I have dragged one of those into the Action Button Prefab of the UnitActionSystemUI component. I have also removed the ‘button’ value from it. The prefab still has the button, but the instance referenced by UnitActionSystemUI is not the prefab; it’s an instance already in the hierarchy. So, UnitActionSystemUI is not referencing the prefab from the assets, it’s referencing an instance of the prefab in the hierarchy. This is a bit of a stretch, but just drag the prefab from the project window into the Action Button Prefab on the UnitActionSystemUI component again to make sure it’s the correct one. If your prefab (not the instances) have a button, it should solve the problem.

2 Likes

That was it, the prefabs in the scene didn’t have the button prefab attached. Thank you so much

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms