Why can't I reference this node?

(Main Scene)
@onready var scoreup_scene = preload(“res://Scenes/score_up.tscn”)
func _on_player_score():
var scoreup = scoreup_scene.instantiate()
if scoreup.AnimatedSprite2D.is_playing(“1”):
score += 100
if scoreup.AnimatedSprite2D.is_playing(“2”):
score += 200
if scoreup.AnimatedSprite2D.is_playing(“3”):
score += 300

(Enemy Script)
@onready var scoreup_scene = preload(“res://Scenes/score_up.tscn”)
func enemy_hit():
health -= 1
if health == 0:
var scoreup = scoreup_scene.instantiate()
var spawnrng = rng.randi_range(1, 3)
if spawnrng == 1:
scoreup.score1 = true
if spawnrng == 2:
scoreup.score2 = true
if spawnrng == 3:
scoreup.score3 = true
scoreup.global_position = global_position
get_tree().get_root().add_child(scoreup)
queue_free()

(Scoreup Script)
func _physics_process(delta):
position.x -= delta * speed
if position.x < 0:
queue_free()
if score1 == true:
$AnimatedSprite2D.play(“1”)
if score2 == true:
$AnimatedSprite2D.play(“2”)
if score3 == true:
$AnimatedSprite2D.play(“3”)

Basically what I’m trying to do is every time an enemy dies, it spawns a random score increase of 100-300, which I’ve created variants of via different animations. They’re spawning fine, but every time I pick them up it gives me this error:
Invalid access to propery or key ‘AnimadedSprite2D’ on a base object of type ‘Area2D (score_up.gd)’

Any insights into what newbie mistake I’m doing? Thank you in advance

Youre getting the error because you cant access nodes by doing scoreup.AnimatedSprite2D, as godot expects anything after the dot to be a variable or a function(or better said, a member).

You need to either define a variable in the scoreup.gd script like so:

var animated_sprite = $AnimatedSprite2D

or alternatively you can use get_node() to get the node in Main Scene like so:

scoreup.get_node("AnimatedSprite2D")
1 Like

Ah, thank you so much!! Referencing is hard to wrap my head around

No problem!

If you want to learn more, i suggest reading the documentation, as it is very helpful.
If you don’t know anything specific, try using the search bar and most of the time it will tell you what exactly it does :3

1 Like

I tried them both and I’m getting the error “Invalid call. Nonexistent function “is_playing” in base ‘nil’”

(Main Scene)
@onready var scoreup_scene = preload(“res://Scenes/score_up.tscn”)
func _on_player_score():
var scoreup = scoreup_scene.instantiate()
if scoreup.animated_sprite.is_playing(“1”):
score += 100
if scoreup.animated_sprite.is_playing(“2”):
score += 200
if scoreup.animated_sprite.is_playing(“3”):
score += 300

(Scoreup scene)
@onready var animated_sprite = $AnimatedSprite2D
func _physics_process(delta):
position.x -= delta * speed
if position.x < 0:
queue_free()
if score1 == true:
$ScoreAnimation.play(“1”)
if score2 == true:
$ScoreAnimation.play(“2”)
if score3 == true:
$ScoreAnimation.play(“3”)

Perhaps is_playing() can only be used with animation player and not sprite 2d? That would be my best guess but I’m not sure…

Indeed it is only on AnimationPlayer(docs)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.