[solved]Found a very odd "A null value was found where an object instance was required" error

Hello everyone,
in the performance reporting tool I noticed a very strange exception that only a couple of users encounter.

The trace is:

EventPanelItem.GoToEventLocation ()
UnityEngine.Events.UnityEvent.Invoke ()
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor)
UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchPress (UnityEngine.EventSystems.PointerEventData pointerEvent, Boolean pressed, Boolean released)
UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchEvents ()

So what’s happening here is, if a new event is triggered at the end of the turn the event manager instantiates a new ui prefab (parent with script, child = button) and then hides it until the player revisits the map.
In this case a user then clicks on the child button element of that newly instantiated object which calls GoToEventLocation() (OnClick event set through the inspector).The method only forwards a Vector3 to the camera.

This error sounds like as if either the parent or its script is missing when the OnClick Event fires which should be impossible because of the nature of the prefab.
I honestly don’t know what I should do here, …help!

Any chance of posting the code for EventPanelItem.GoToEventLocation() ? That method is being called, so the script is there. Something must be null in an unexpected way, maybe a reference caching issue?

Sure it’s:

Camera.main.GetComponent<RTS_NewSyntaxe>().MoveToPosition(tilePos);

tilePos is a Vector3

Either camera is null or component is null.

Also, avoid using Camera.main, cache the reference.

Possible Camera.main is null because it’s not tagged as the main camera properly. But as mentioned, Camera.main should be cache for use.

Do you have more than one camera? Say, a UI camera? If more than one camera is tagged “MainCamera” by accident, it may be returning one that doesn’t have RTS_NewSyntaxe script. There’s no guarantee that Camera.main will return the same camera each time if there is more than one camera with the right tag.

Jep there is another one but only one is active and the UI element with that button gets disabled before the switch.
I guess either the disabled camera gets returned in some cases or there is some sort of severe lag between the clicking the event button and switching the cameras that causes it to execute after the switch.

In any way, thx everyone for giving me the right pointers, I should be able to fix this now!