NullReferenceException

NullReferenceException: Object reference not set to an instance of an object
UnitSelectedVisual.Start () (at Assets/Scripts/UnitSelectedVisual.cs:19)

This is the code at line 19
private void Start()

{

    UnitActionSystem.Instance.OnSelectedUnitChanged +=UnitActionSystem_OnSelectedUnitChanged;

    UpdateVisual();

}

Any Ideas would help. I watch the videos twice. Couldn’t find anything wrong.

It is good practice to recognize potential errors early on. I’m not familiar with your code or the course content, but I can help you identify the problem. NullReferenceExceptions basically means that the reference to the reference type you are trying to access is invalid, so you better catch it as follows:

private void Start()
{
    if (UnitActionSystem.Instance != null) 
    {
        UnitActionSystem.Instance.OnSelectedUnitChanged +=UnitActionSystem_OnSelectedUnitChanged;
    }

    UpdateVisual();
}

If this does not throw any errors, then your issue is related to the UnitActionSystem

It definitely looks like your UnitActionSystem hasn’t set it’s instance quite yet. This is known as a race condition. It happens when two competing initialization methods happen out of order. In this case, your UnitSelectedVisual is looking for the instance in UnitActionSystem, but the UnitActionSystem hasn’t run it’s code yet.

We should be initializing the UnitActionSystem in it’s Awake() method, which should always be ran before any of the Start() methods have run, regardless of the Script Operation order.

Paste in your UnitActionSystem’s Awake() and Start() methods, and we’ll take a look.

I was wondering if it had to do with the prefab, so I posted a picture of it as well as the code.
Screenshot 2024-04-06 185127

This is the UnitSelectedVisual. Can I see the UnitActionSystem.

Rather than pasting in a Screenshot, follow the instructions Here to paste in the actual text of the script.

I just want to take a moment and thank you Brian for all you help.
Here is the code.

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

public class UnitActionSystem : MonoBehaviour
{

    public static UnitActionSystem Instance { get; private set; }


    public event EventHandler OnSelectedUnitChanged;


    [SerializeField] private Unit selectedUnit;
    [SerializeField] private LayerMask unitLayerMask;


    private void Awake()
    {
        if (Instance != null)
        {
            Debug.LogError("There's more than one UnitActionSystem! " + transform + " - " + Instance);
            Destroy(gameObject);
            return;
        }
        Instance = this;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (TryHandleUnitSelection()) return;

            selectedUnit.Move(MouseWorld.GetPosition());
        }
    }

    private bool TryHandleUnitSelection()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out RaycastHit raycastHit, float.MaxValue, unitLayerMask))
        {
            if (raycastHit.transform.TryGetComponent<Unit>(out Unit unit))
            {
                SetSelectedUnit(unit);
                return true;
            }
        }

        return false;
    }

    private void SetSelectedUnit(Unit unit)
    {
        selectedUnit = unit;

        OnSelectedUnitChanged?.Invoke(this, EventArgs.Empty);
    }

    public Unit GetSelectedUnit()
    {
        return selectedUnit;
    }

}

That’s interesting… there shouldn’t be a race condition here, as the UnitActionSystem.Awake() should always be running before any Start() methods…

Are you using Play Mode Options in the Project Settings | Editor section? With Singletons, it’s important to make sure that Reload Domain is ticked or the UnitActionSystem could detect that there is an instance when there is not really one there. It’s generally best NOT to use Enter Play Mode Options

1 Like

Unless, and hear me out, there is no UnitActionSystem in the scene. But it feels to obvious

As soon as you mentioned it, suddenly seemed possible, but I think other scripts also rely on UnitActionSystem as well… not enough error messages

1 Like

I think I am going back to were we created the unit prefab, it all worked up to there. I’ll see what happens. I thank you guys for your work on this.

1 Like

Brian: You where absolutely right. I don’t know why or how, but the UnitActionSystem was missing from scene. I know I added it along with the course said to. Again thanks for your help.

Actually, I’m going to hand that Solution check mark to @bixario, his suggestion led me to the realization that this was likely the problem.

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

Privacy & Terms