Invalid access to property key error

Godot Version

Godot 4.7

Question

I keep getting this error “Invalid access to property or key ‘current_target’ on a base object of type ‘Node3D’.”

im using a ray cast node for this code

func _physics_process(delta: float) → void:
if is_colliding():
current_target = get_collider()
print(current_target)
location = current_target.get_global_position()
else:
current_target = “none”
print(location)

and a sprite 3d for this

extends Sprite3D
@onready var pickup = get_parent()
var current_target = “current_target”
func _process(delta: float) → void:
current_target = pickup.current_target
if current_target != “none”:
set_global_position(pickup.loction)
print(pickup.location)

the spirte3D is a child of the ray cast

You don’t have a current_target variable in the first script, that’s on the second script

Also please format your code so it can be read easily, use three backtics ``` like this

I’ve changed it to look like this now but its still not working

extends RayCast3D
@export var current_target = “none”
@export var location = “none”
func _physics_process(delta: float) → void:
if is_colliding():
current_target = get_collider()
print(current_target)
location = current_target.get_global_position()
else:
current_target = “none”
print(location)

So it’s a Node3D error, so whatever get_parent() is handing back to the Sprite3D isn’t your RayCast3D, it’s a plain Node3D. That’s why adding the variable to the raycast script didn’t change anything, the sprite is reading off a different node.

I think you need to find out if there’s a Node3D sitting between the two in the scene tree, since it only takes one node in between for get_parent() to land somewhere you didn’t expect. If that’s it you could chain another get_parent(), though an @export var raycast: RayCast3D you drag in from the inspector won’t break if you move things around later.

Also this part in the Sprite3D script is missing an a: set_global_position(pickup.loction)

scene tree looks like this

when i dragged the ray cast 3d in it gave me this

@onready var ray_cast_3d: RayCast3D = $“..”

wich is giving me this error “Trying to assign value of type ‘Node3D’ to a variable of type ‘RayCast3D’.”

func _process(delta: float) → void:
var current_target = ray_cast_3d.current_target
if current_target != “none”:
set_global_position(pickup.location)

Did you check the tree during runtime to see if anything is dynamically inserting itself as the parent or if the sprite3d is getting reparented?

I had an instance of it in my level scene tree and my player scene tree that why it was trying to assign to a node3d