Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Whats wrong with my code

A topic by spraypaintdev created 8 days ago Views: 156 Replies: 2
Viewing posts 1 to 5

Ok so I use godot 4.7 and i keep getting this error and i dont know how to fix it

Invalid access to property or key 'anim' on a base object of type 'Node3D (AK-47.tscn::GDScript_pl6ux)'.

heres one of my weapons code

extends Node3D

@export var damage = 1.0

@onready var anim = $AnimationPlayer

@onready var shot_sound = $shot_sound

@onready var raycast = $RayCast3D

var can_shoot = false

func _process(sdelta: float) -> void:

if Input.is_action_just_pressed("Shoot") and can_shoot and not anim.is_playing():

print("something else wrong")

anim.play("shoot")

shot_sound.play()

if raycast.is_colliding():

if raycast.get_collider().is_in_group("enemy"):

raycast.get_collider().hp -= damage

func _on_animation_player_animation_finished(anim_name: StringName) -> void:

if anim_name == "shoot":

can_shoot = true

elif anim_name == "Equip":

can_shoot = true

visible = true

elif anim_name == "Unequip":

can_shoot = false

visible = false

and heres my players hand code(where theres apparently a problem

extends Node3D

var weapons = []

var selected_weapon = 0

# Called when the node enters the scene tree for the first time.

func _ready() -> void:

for w in self.get_children():

weapons.append(w)

w.visible = false

w.anim.play("Unequip")

weapons[selected_weapon].visible = true

weapons[selected_weapon].anim.play("Equip")

# Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(delta: float) -> void:

if Input.is_action_just_pressed("Next_Weapon"):

weapon_change(1)

elif Input.is_action_just_pressed("Previous_Weapon"):

weapon_change(-1)

func weapon_change(dir):

weapons[selected_weapon].anim.play("Unequip")

selected_weapon += dir

if selected_weapon < 0:

selected_weapon += weapons.size()

elif selected_weapon >= weapons.size():

selected_weapon -= weapons.size()

weapons[selected_weapon].visible = true

weapons[selected_weapon].anim.play("Equip")


thanks

Moderator moved this topic to General Development
Moderator

(moved to the right category)

That error usually means the node in your loop has no 'anim' variable, so either that child does not have the weapon script attached, or the script failed to parse and loaded without its variables. Check the Errors tab and the weapon scene for a red script warning first. Then print w.name and w.get_script() inside the loop in _ready to see which child breaks. It also helps to give the weapon script a class_name and check 'if w is Weapon' before touching w.anim.