How do I solve "Invalid access to property or key 'transform' on a base object of type 'Nil'."?

I am trying to make a fps controller and I tried adding camera rotation but I am getting this error “Invalid access to property or key ‘transform’ on a base object of type ‘Nil’.”

This is the Fps Controller:
extends CharacterBody3D

const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@export var Mouse_Sensitivity : float = 0.5
@export var Tilt_Lower_Limit := deg_to_rad(-90.0)
@export var Tilt_Upper_Limit := deg_to_rad(90.0)
@export var Camera_Controller : Camera3D

var _mouse_input : bool = false
var _mouse_rotation : Vector3
var _rotation_input : float
var _tilt_input : float
var _player_rotation : Vector3
var _camera_rotation : Vector3

func _unhandled_inpiut(event):
_mouse_input = event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
if _mouse_input:
_rotation_input = -event.relative.x * Mouse_Sensitivity
_tilt_input = -event.relative.y * Mouse_Sensitivity
print(Vector2(_rotation_input,_tilt_input))

func _update_camera(delta):
_mouse_rotation.x += _tilt_input * delta
_mouse_rotation.x = clamp(_mouse_rotation.x, Tilt_Lower_Limit, Tilt_Upper_Limit)
_mouse_rotation.y += _rotation_input * delta

_player_rotation = Vector3(0.0,_mouse_rotation.y,0.0)
_camera_rotation = Vector3(_mouse_rotation.x,0.0,0.0)

Camera_Controller.transform.basis = Basis.from_euler(_camera_rotation)
Camera_Controller.rotation.z = 0.0

global_transform.basis = Basis.from_euler(_player_rotation)

_rotation_input = 0.0
_tilt_input = 0.0

func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _physics_process(delta: float) → void:
# Add the gravity.
var _input := Vector3.ZERO
if not is_on_floor():
velocity += get_gravity() * delta

_update_camera(delta)

# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_backwards")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
	velocity.x = direction.x * SPEED
	velocity.z = direction.z * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)

move_and_slide()

Anyone know what I did wrong?

Can you highlight the line that causes this error?

I think
it might be this

OK, it seems that Camera_Controller isn’t a valid node. Can you verify that it really exists in the scene?

By the way, there is a typo in this function name:

First, format your code correctly, the way the code is now is hard to follow, select the text related to code and press ctrl + e to format correctly.

Second, when you have an error, show where the error is happening, debbuger will point the line and what is wrong.


From your code i guess the problem happens here:

You declare Camera_Controller here, you setted that on editor?

For some more context its not showing any errors and its showing this

That’s Camera_Controller assigned through variable and at line:

  • Camera_Controller.transform.basis = Basis.from_euler(_camera_rotation) -

Referencing to Camera_Controller as Camera3D is an Node and “transform” it’s an property or attribute of Camera3D which it’s inherited from Node3D or Spatial in Godot 3.x.

“transform” is nothing or null value because at least add there Camera3D.new() as instancing from Camera3D node or class and you can referencing that the “transform” property of Node3D or Spatial node in Godot 3.x

  • Camera_Controller.transform.basis = Basis.from_euler(_camera_rotation) -

“transform” is through dot connector to “basis” or actually the “Transform3D” node or class it’s all the class must instancing first through .new() method.

  • @export var Camera_Controller : Camera3D -

Here’s how assignment could failed and “transform” is nothing or null type value such error above.

  • var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() -

But here “transform” are really property of same class extended with “CharacterBody3D” that’s why nothing problem here.

if your still having this problem then go to the root node of the player scene in the inspector under player.gd assign the camera controller to camera3D

image

that should fix it

I’m having the same mistake🤧