Godot Version
4.6
Question
I’m trying to create an health bar system but im getting the error:
Attempt to call function ‘get_children’ in base ‘null instance’ on a null instance.
here’s my scene structure incase it’s the problem:
extends CharacterBody2D
class_name PlayerController
**@onready var health_parent: HBoxContainer = $Visuals/Health/HBoxContainer**
@onready var animation_player: AnimationPlayer = $Animation_Controls/AnimationPlayer
@onready var currenthealth: int = max_health
@onready var particles_player: CPUParticles2D = $Particles/CPUParticles2D
# Movement Variables
@export var acceleration := 2400.0
@export var speed : float = 300.0
@export var jump_force : float = -250.0
@export var jump_time : float = 0.25
@export var coyote_time : float = 0.075
@export var gravity_multiplier : float = 3.0
@export var max_health = 3
var reset_position : Vector2
var direction = 0
var is_jumping : bool = false
var jump_timer : float = 0
var coyote_timer : float = 0
var can_control : bool = true
var can_take_damage : bool = true
**var hearts_list : Array[TextureRect]**
func _ready():
reset_position = Vector2(3.0, -63.0)
**var hearts_parent = health_parent**
** for child in hearts_parent.get_children():** -- THIS IS THE PROBLEM
** hearts_list.append(child)**
** print(hearts_list)**
**func update_hearts():**
** for i in range(hearts_list.size()):**
** hearts_list[i].visible = i < currenthealth**
func _physics_process(delta: float) -> void:
if not can_control: return
# Add the gravity.
if not is_on_floor() and not is_jumping:
velocity += get_gravity() * gravity_multiplier * delta
coyote_timer += delta
else:
coyote_timer = 0
# Handle jump.
if Input.is_action_just_pressed("Jump") and (is_on_floor() or coyote_timer < coyote_time):
particles_player.emitting = true
velocity.y = jump_force
is_jumping = true
elif Input.is_action_pressed("Jump") and is_jumping:
velocity.y = jump_force
if is_jumping and Input.is_action_pressed("Jump") and jump_timer < jump_time:
jump_timer += delta
else:
is_jumping = false
jump_timer = 0
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
direction = Input.get_axis("Left", "Right")
velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
move_and_slide()
func handle_danger():
currenthealth -= 1
if currenthealth == 0:
velocity.x = 0
print("You Died")
visible = false
can_control = false
await get_tree().create_timer(1).timeout
reset_player()
return
func reset_player() -> void:
_death_animation()
currenthealth = 3
velocity.x = 0
global_position = reset_position
visible = true
can_control = true
func _death_animation():
var tween = create_tween()
tween.tween_property(self, "light_mask", 500, 2 )


