0

enter code hereI am working on AR game, using AR foundation, whenever I run it I get the error: NullReferenceException: Object reference not set to an instance of an object UIManager.Update () , I had it solved by referencing the object in the scene, but I get another error: NullReferenceException: Object reference not set to an instance of an object EventManager.TriggerEvent (System.String eventName, System.Collections.Generic.Dictionary`2[TKey,TValue]

I don't really know how to deal with that and this is the code:

private static EventManager eventManager;

public static EventManager instance
{
    get
    {
        if (!eventManager)
        {
            eventManager = FindObjectOfType(typeof(EventManager)) as EventManager;

            if (!eventManager)
            {
                Debug.LogError("There needs to be one active EventManger script on a GameObject in your scene.");
            }
            else
            {
                eventManager.Init();
            }
        }
        return eventManager;
    }
}

void Init()
{
    if (eventDictionary == null)
    {
        eventDictionary = new Dictionary<string, Action<Dictionary<string, object>>>();
    }
}

public static void StartListening(string eventName, Action<Dictionary<string, object>> listener)
{
    Action<Dictionary<string, object>> thisEvent = null;

    if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
    {
        // Add the called function to the action
        thisEvent += listener;
        // Renew the dictionary with the action
        instance.eventDictionary[eventName] = thisEvent;
    }
    else
    {
        thisEvent += listener;
        instance.eventDictionary.Add(eventName, thisEvent);
    }
}

public static void StopListening(string eventName, Action<Dictionary<string, object>> listener)
{
    if (eventManager == null) return;
    Action<Dictionary<string, object>> thisEvent = null;
    if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
    {
        thisEvent -= listener;
        instance.eventDictionary[eventName] = thisEvent;
    }
}

public static void TriggerEvent(string eventName, Dictionary<string, object> message)
{
    Action<Dictionary<string, object>> thisEvent = null;
    if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
    {
        thisEvent.Invoke(message);
    }
}

}

safae
  • 11
  • 2
  • I am pretty sure its not the issue about updating your AR foundation. Null reference is like whats its name. it cannot find one of the object or the object is not properly assigned. use the Unity console, and double click your error. it will show you exactly where the null reference coming from – Hikari Apr 19 '23 at 14:35
  • I suggest you take a look at [this](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) other question about NullReferenceException – Cyclone6664 Apr 19 '23 at 15:16
  • Just a sidenote: This should actually be enough already `if(!blendShape) blendShape = GameObject.FindFirstObjectByType();` .. it will of course return `null` if no according component exists in the Scene or is inactive -> maybe the updated version deactivates your object if it isn't tracked? .. and then check if any of the serialized fields is not referenced .. not much more we can do from here ... in general you shouldn't poll information and apply to display text each frame ... go event driven and update the display only if values actually changed – derHugo Apr 19 '23 at 16:12
  • Indeed! I have solved the issue in this code, I need the object referenced in the scene, however I have another null reference my another code, that I don't really know how to deal with the object, – safae Apr 20 '23 at 01:31

0 Answers0