Invalid access to property or key 'rotation' on a base object of type 'null instance'

I tried making a simple camera rotation script with lerp.

extends Camera3D

@onready var player: CharacterBody3D = $"../.."
@onready var camera_3d: Camera3D = $"Camera3D"
@export var rot_speed: float = PI/2

func _ready() -> void:
	pass
func _process(delta):
	lerp_angle(camera_3d.rotation.y, player.rotation.y, delta * rot_speed)

The error message points to line 10, the lerp_ angle.

 _process: Invalid access to property or key 'rotation' on a base object of type 'null instance'.
  <GDScript Source>camera_3d.gd:10 @ _process()
  <Stack Trace> camera_3d.gd:10 @ _process()

Can anybody explain and help fix this? I only recently started using Godot and I’ll be so thankful if anybody can help.

One of your node paths is probably incorrect, likely $“Camera3D” if the script is attached to that camera.

1 Like

That means either the camera_3d variable or the player variable has not been set with an actual object. The reference is null.
Does the error appear only once when you play, or does it appear every frame?

I noticed your script extends camera3D. Why are you getting a reference to a Camera3D if the script itself is the Camera3D?

Also, I believe it would be better to rotate the camera in physics process instead of process. I know that’s not a rule for all cases, but usually you’ll move the player in physics process and if the camera depends on player movement it’s better to move it on physics process so it’s in sync. Someone correct me if I’m wrong. [Edit: I was wrong]

Shouldn’t matter and I’d actually argue it’s better to have it in the _process() than _physics_process(). Your camera’s position and movement matters only when something happens visually on the screen, which is when the _process() fires. If you’re updating the camera’s position in the _physics_process(), you are likely updating it more times than necessary, because you’re updating its position between the visual frames, which doesn’t make sense for a camera.

The difference is probably very negligeable though and other than this performance reason, I don’t see any other issues with either approach.

2 Likes

Or less than needed. Depends on the ratio of rendered frames vs physics ticks.
Yeah, it’s generally better to update the camera is _process() as that will guarantee that each rendered frame will get the properly updated camera.

1 Like

Good point, in certain scenarios it could be even lagging behind, thanks for pointing that out.

1 Like

Good points. I stand corrected.
Thanks @normalized and @wchc.