what is wrong in this code it is saying invalid get index 'rotation' (on base: 'null instance').

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By invisibletorch
extends KinematicBody


export var speed := 7.0
export var gravity := 50.0
export var jump_strength := 20.0

var  _velocity := Vector3.ZERO
var _snap_vector := Vector3.DOWN

onready var _spring_arm: SpringArm = $SpringArm
onready var _model: Spatial = $CSGCylinder

func _physics_process(delta: float) -> void:
	
	var move_direction := Vector3.ZERO
	move_direction.x = Input.get_action_strength("move_right") - 
        Input.get_action_strength("move_left")
	move_direction.z = Input.get_action_strength("move_back") - 
        Input.get_action_strength("move_front")
	move_direction = move_direction.rotated(Vector3.UP, 
        _spring_arm.rotation.y).normalized()
	
	
	_velocity.x = move_direction.x * speed
	_velocity.z = move_direction.z * speed
	_velocity.y -= gravity * delta


	var just_landed := is_on_floor() and _snap_vector == Vector3.ZERO
	var is_jumping := is_on_floor() and Input.is_action_just_pressed ("jump")
	if is_jumping:
		_velocity.y = jump_strength
		_snap_vector= Vector3.ZERO
	elif just_landed:
		_snap_vector = Vector3.DOWN
		_velocity = move_and_slide_with_snap(_velocity, _snap_vector, Vector3.UP, true)

func process (_delta: float) -> void:
	_spring_arm.translation = translation

the code is b/w extends to translation i dont know that part is not showing as code

Edited to fix code formatting…

jgodfrey | 2023-02-14 03:15

:bust_in_silhouette: Reply From: jgodfrey

So, looking at the posted code, I assume the problem is here since it’s the only reference to rotation.

move_direction = move_direction.rotated(Vector3.UP,
    _spring_arm.rotation.y).normalized()

And, the error means that the object on which you’re trying to access the rotation property is null. So, looking at how you’re getting the object reference, I see this:

onready var _spring_arm: SpringArm = $SpringArm

And, while that looks reasonable, the $SpringArm reference will only work if a node named SpringArm is a direct child of the node containing the above script. If that’s not the case, this will result in a null reference, and ultimately to the error you reported.

So, your scene tree must not be organized properly for that reference to work.

Bottom line, you’ll need to adjust that reference to properly access the SpringArm node.

thanks for answering

invisibletorch | 2023-02-14 11:59

i didnt understand what excatly what i have to do

invisibletorch | 2023-02-14 12:28

The problem is here:

onready var _spring_arm: SpringArm = $SpringArm

Specifically, that $SpringArm reference. As I mentioned above, for that to work it’d require that a node named SpringArm is a direct child of whatever node contains the code you posted. I can only assume that’s not the case. So, if your SpringArm node is not a direct child, you’ll need to change the $SpringArm reference to properly access the node in the scene tree.

If that still doesn’t make sense, we need to know what your scene tree looks like. And, which node your script is attached to…

jgodfrey | 2023-02-14 14:30