NullReferenceException on property that was selected in Inspector

Hey there everyone, I’m new to Unity but do have a background from backend web development. That aside, I’m having issues with trying to instantiating the ARSession on my app on a button press. Even though in Inspector, I’ve already assigned the ARSession object, in the script itself it returns itself as Null, so I just don’t get it. May I know what I’m doing wrong, please?

(Please do ignore the other properties… they are there for temporary/testing for now)

The code in question:

Error log when pressing on the button:

2023/07/17 22:59:30.532 19310 19368 Info Unity ToggleCamera() called!
2023/07/17 22:59:30.532 19310 19368 Info Unity Function:ToggleCamera()
2023/07/17 22:59:30.532 19310 19368 Info Unity UnityEngine.Events.UnityEvent:Invoke()
2023/07/17 22:59:30.532 19310 19368 Info Unity UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
2023/07/17 22:59:30.532 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointerButton(ButtonState&, PointerEventData)
2023/07/17 22:59:30.532 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointer(PointerModel&)
2023/07/17 22:59:30.532 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:Process()
2023/07/17 22:59:30.532 19310 19368 Info Unity UnityEngine.EventSystems.EventSystem:Update()
2023/07/17 22:59:30.532 19310 19368 Info Unity
2023/07/17 22:59:30.535 19310 19368 Info Unity ToggleCamera() called! camera start
2023/07/17 22:59:30.535 19310 19368 Info Unity Function:ToggleCamera()
2023/07/17 22:59:30.535 19310 19368 Info Unity UnityEngine.Events.UnityEvent:Invoke()
2023/07/17 22:59:30.535 19310 19368 Info Unity UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
2023/07/17 22:59:30.535 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointerButton(ButtonState&, PointerEventData)
2023/07/17 22:59:30.535 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointer(PointerModel&)
2023/07/17 22:59:30.535 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:Process()
2023/07/17 22:59:30.535 19310 19368 Info Unity UnityEngine.EventSystems.EventSystem:Update()
2023/07/17 22:59:30.535 19310 19368 Info Unity
2023/07/17 22:59:30.537 19310 19368 Info Unity 1000
2023/07/17 22:59:30.537 19310 19368 Info Unity Function:ToggleCamera()
2023/07/17 22:59:30.537 19310 19368 Info Unity UnityEngine.Events.UnityEvent:Invoke()
2023/07/17 22:59:30.537 19310 19368 Info Unity UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
2023/07/17 22:59:30.537 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointerButton(ButtonState&, PointerEventData)
2023/07/17 22:59:30.537 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointer(PointerModel&)
2023/07/17 22:59:30.537 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:Process()
2023/07/17 22:59:30.537 19310 19368 Info Unity UnityEngine.EventSystems.EventSystem:Update()
2023/07/17 22:59:30.537 19310 19368 Info Unity
2023/07/17 22:59:30.539 19310 19368 Info Unity 2000
2023/07/17 22:59:30.539 19310 19368 Info Unity Function:ToggleCamera()
2023/07/17 22:59:30.539 19310 19368 Info Unity UnityEngine.Events.UnityEvent:Invoke()
2023/07/17 22:59:30.539 19310 19368 Info Unity UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
2023/07/17 22:59:30.539 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointerButton(ButtonState&, PointerEventData)
2023/07/17 22:59:30.539 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointer(PointerModel&)
2023/07/17 22:59:30.539 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:Process()
2023/07/17 22:59:30.539 19310 19368 Info Unity UnityEngine.EventSystems.EventSystem:Update()
2023/07/17 22:59:30.539 19310 19368 Info Unity
2023/07/17 22:59:30.540 19310 19368 Info Unity Null
2023/07/17 22:59:30.540 19310 19368 Info Unity Function:ToggleCamera()
2023/07/17 22:59:30.540 19310 19368 Info Unity UnityEngine.Events.UnityEvent:Invoke()
2023/07/17 22:59:30.540 19310 19368 Info Unity UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
2023/07/17 22:59:30.540 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointerButton(ButtonState&, PointerEventData)
2023/07/17 22:59:30.540 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointer(PointerModel&)
2023/07/17 22:59:30.540 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:Process()
2023/07/17 22:59:30.540 19310 19368 Info Unity UnityEngine.EventSystems.EventSystem:Update()
2023/07/17 22:59:30.540 19310 19368 Info Unity
2023/07/17 22:59:30.542 19310 19368 Info Unity 3000
2023/07/17 22:59:30.542 19310 19368 Info Unity Function:ToggleCamera()
2023/07/17 22:59:30.542 19310 19368 Info Unity UnityEngine.Events.UnityEvent:Invoke()
2023/07/17 22:59:30.542 19310 19368 Info Unity UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
2023/07/17 22:59:30.542 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointerButton(ButtonState&, PointerEventData)
2023/07/17 22:59:30.542 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointer(PointerModel&)
2023/07/17 22:59:30.542 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:Process()
2023/07/17 22:59:30.542 19310 19368 Info Unity UnityEngine.EventSystems.EventSystem:Update()
2023/07/17 22:59:30.542 19310 19368 Info Unity
2023/07/17 22:59:30.545 19310 19368 Info Unity Null
2023/07/17 22:59:30.545 19310 19368 Info Unity Function:ToggleCamera()
2023/07/17 22:59:30.545 19310 19368 Info Unity UnityEngine.Events.UnityEvent:Invoke()
2023/07/17 22:59:30.545 19310 19368 Info Unity UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
2023/07/17 22:59:30.545 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointerButton(ButtonState&, PointerEventData)
2023/07/17 22:59:30.545 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointer(PointerModel&)
2023/07/17 22:59:30.545 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:Process()
2023/07/17 22:59:30.545 19310 19368 Info Unity UnityEngine.EventSystems.EventSystem:Update()
2023/07/17 22:59:30.545 19310 19368 Info Unity
2023/07/17 22:59:30.547 19310 19368 Info Unity 4000
2023/07/17 22:59:30.547 19310 19368 Info Unity Function:ToggleCamera()
2023/07/17 22:59:30.547 19310 19368 Info Unity UnityEngine.Events.UnityEvent:Invoke()
2023/07/17 22:59:30.547 19310 19368 Info Unity UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
2023/07/17 22:59:30.547 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointerButton(ButtonState&, PointerEventData)
2023/07/17 22:59:30.547 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:ProcessPointer(PointerModel&)
2023/07/17 22:59:30.547 19310 19368 Info Unity UnityEngine.InputSystem.UI.InputSystemUIInputModule:Process()
2023/07/17 22:59:30.547 19310 19368 Info Unity UnityEngine.EventSystems.EventSystem:Update()
2023/07/17 22:59:30.547 19310 19368 Info Unity
2023/07/17 22:59:30.555 19310 19368 Error Unity NullReferenceException: Object reference not set to an instance of an object.
2023/07/17 22:59:30.555 19310 19368 Error Unity   at Function.ToggleCamera () [0x00000] in <00000000000000000000000000000000>:0
2023/07/17 22:59:30.555 19310 19368 Error Unity   at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <00000000000000000000000000000000>:0
2023/07/17 22:59:30.555 19310 19368 Error Unity   at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) [0x00000] in <00000000000000000000000000000000>:0
2023/07/17 22:59:30.555 19310 19368 Error Unity   at UnityEngine.InputSystem.UI.InputSystemUIInputModule.ProcessPointerButton (UnityEngine.InputSystem.UI.PointerModel+ButtonState& button, UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in <00000000000000000000000000000000>:0
2023/07/17 22:59:30.555 19310 19368 Error Unity   at UnityEngine.InputSystem.UI.InputSystemUIInputModule.ProcessPointer (UnityEngine.InputSystem.UI.PointerModel& state) [0x00000] in <00000000000000000000000000000000>:0
2023/07/17 22:59:30.555 19310 19368 Error Unity   at UnityEngine.InputSystem.UI.InputSystemUIInputModule.Process () [0x00000] in <00000000000000

You likely have another copy of the script in the scene (or on a prefab) in which the field is not assigned.

Thanks for the quick reply! Though may I know what you mean that I’ve got another copy of the script in the scene? Is there a way to check and find out?

Why are you posting an error log instead of just the message in the console? Are you getting this error in a build but not in play mode?

Put a Debug.Log in your start to print the id or object name or something. See if there are two outputs in the console.

Could it also be possible that the AR Session object has been deleted or your reference was assigned to null at some point?

Sorry, ain’t too sure what you mean, but if the logs were too long then I’m sorry, I’ll keep in mind to post only the error message next time. In regards to where I’m getting this error… I’ve only tested this on my device by doing the “Build and Run”, not too sure what play mode is.

To be honest, I’m not sure, I made the ARSession object in the hierarchy, didn’t do too much to it, then I attached it to the script’s parameter. And then after that as you saw in my screenshot, I tried to enabled it and that was about it.

There’s a little “play” button at the top of your editor window which you can use to play your project without going through the trouble of building it. Any runtime messages appear in the console so you can see them as soon as they pop-up. You can click on the message and unity will usually highlight the affected object in the scene hierarchy.

It’s much easier to troubleshoot in playmode then from a build.

Hmm, it seems when I try to “play” the project, this message pops out: No active UnityEngine.XR.XRInputSubsystem is available. Please ensure that a valid loader configuration exists in the XR project settings.

I searched Google and found out that I need to install XR Simulator in order to get this working (which led me to XR Interaction Toolkit), however, I installed the toolkit and it seems like… nothing changed for the Plug-in Providers, I can’t simulate the scene as instructed here: https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.0/manual/xr-simulation/simulation-getting-started.html

Am I doing something wrong or missing a step somewhere?

You can check for references in a Scene by right clicking the script, in your case, and selecting “Find References In Scene” from the menu. Or typ the name of the script in the top searchbar (in Hierarchy panel) directly.

To close the searchresults click the tiny x in the searchbar at the top.

Ok, I didn’t realize you were developing for this type of environment. Unfortunately I don’t know anything about Unity’s XR platform. It sounds like you are on the right track, but you may want to try the AR/VR forum to get this working. There may be more people with AR knowledge that might see your post.