Wrong _ready() order

Godot Version

4.2.1

Question

So i instantiate a PackedScene through code and give it a value.

#Script_1

_func ready():
  var player = scene.instantiate()
  add_child(player)
  player.hp = 100
#Player script

var hp

func _ready():
  print(hp)

I dont get why player prints me a “null”. Any ideas?

_ready is called when the node is added to the scene tree, which happens when you call add_child in your first script.

Try moving the line player.hp = 100 to be above the line add_child(player).

2 Likes

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