Quick answer: Screen Space Overlay canvases always have pressEventCamera = null. Camera-based or World Space canvases return their Event Camera. For drag-to-world conversion, fall back to Camera.main when pressEventCamera is null.

Here is how to fix Unity PointerEventData wrong camera. You implement drag-and-drop UI that projects to the world — click an inventory icon, drag over the world, get the world-space hit point under the cursor. You use eventData.pressEventCamera.ScreenPointToRay. NullReferenceException. Or the ray uses the wrong camera and your drop position is off by hundreds of meters.

The Symptom

UI event handlers that need a camera reference encounter one of:

What Causes This

Overlay canvases have no camera. Screen Space - Overlay is rendered directly to the screen without a camera. Unity sets pressEventCamera to null for these canvases. Any screen-to-world math fails.

Event Camera not assigned. For Screen Space - Camera canvases, the Event Camera field on Canvas must be set. If null, pointer events have no camera reference.

Multiple cameras competing. Scenes with several cameras (main, minimap, UI) can cause events to fire for the wrong camera if canvas Event Camera assignments are inconsistent.

Camera.main returns null. Camera.main finds the first enabled camera tagged MainCamera. If no camera has that tag, returns null. Your fallback breaks on scenes where you forgot to tag the camera.

The Fix

Step 1: Always null-check and fall back.

using UnityEngine;
using UnityEngine.EventSystems;

public class InventoryDrag : MonoBehaviour, IDragHandler
{
    public void OnDrag(PointerEventData eventData)
    {
        Camera cam = eventData.pressEventCamera;
        if (cam == null) cam = Camera.main;
        if (cam == null) return; // no viable camera

        Ray ray = cam.ScreenPointToRay(eventData.position);
        if (Physics.Raycast(ray, out RaycastHit hit))
        {
            UpdateDragPreview(hit.point);
        }
    }
}

Two-tier fallback handles both Overlay canvas users (no event camera) and camera-based canvas users (event camera set).

Step 2: Assign Event Camera consistently. For every non-Overlay canvas in your scene, assign the same camera to Event Camera — usually the main gameplay camera. This makes eventData.pressEventCamera predictable.

Step 3: Switch to Screen Space Camera mode. If your UI is screen-space but needs camera context, switch from Overlay to Camera mode:

  1. Canvas Render Mode: Screen Space - Camera
  2. Render Camera: main camera
  3. Plane Distance: 100 or so (distance in front of camera)

Now pressEventCamera is always your main camera, and screen-space UI still appears normal.

Step 4: Use RaycastResult.module.eventCamera for precise camera. PointerEventData includes RaycastResults from every raycaster that found the pointer. Each result’s module (the Raycaster component) has an eventCamera:

foreach (var hit in eventData.hovered)
{
    // Component hierarchy hit here
}

// Better: use the current raycast result's camera
Camera rayCam = eventData.pointerCurrentRaycast.module?.eventCamera;

For hit-testing within a specific canvas’s context, this is the most accurate camera to use.

Multi-Camera Tips

For a game with minimap and main views:

Each canvas stays associated with its intended camera. PointerEventData.pressEventCamera returns the correct one for each click location.

“pressEventCamera is only as smart as your canvas setup. Assign Event Camera explicitly; never rely on magic defaults.”

Related Issues

For World Space canvas issues, see World Space Canvas Not Following Camera. For raycaster blocking, UI Raycaster Blocking Clicks.

Null-check pressEventCamera. Fall back to Camera.main. Assign Event Camera consistently.