Add child causing errors

Godot Version

4.2
hello i am trying to code a way for a pong paddle to get bigger or smaller acording to its health function the only way i could think to do this is by having a buch of paddle segment that i would use the remove child function to hide and then the add child function to add back but every time i call the add child function the game just crashes

here is the code im using

extends CharacterBody2D
@export var speed = 200

var health =3

func get_input():
var input_direction = Input.get_axis(“left”, “right”)
velocity.x = input_direction * speed

func _physics_process(_delta):
get_input()
move_and_slide()#how i control paddle movement ^
if health <= 4 :
remove_child($paddlesegement5)
if health <=5 :
add_child($paddlesegement5)
if Input.is_action_just_pressed(“test input”):
health = health +5

and here is the error i get

E 0:00:03:0229 paddle.gd:14 @ _physics_process(): Parameter “p_child” is null.
<C++ Source> scene/main/node.cpp:1600 @ remove_child()
paddle.gd:14 @ _physics_process()

E 0:00:03:0229 paddle.gd:14 @ _physics_process(): Node not found: “paddlesegement5” (relative to “/root/main level scene/Paddle/Paddle”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1792 @ get_node()
paddle.gd:14 @ _physics_process()

any help solving the errors or telling me a smarter way to do what im trying to do would be much apreciated

The dollar sign notation looks for a child named “paddlesegement5” every time it is called. Since this is on _physics_process that means it may look for a child twice per frame. As you are removing the child “paddlesegement5” it will no longer be found. Maybe you want to store store this node, so that you do not have to look through children every frame.

@onready var segment = $paddlesegement5

func _physics_process(_delta: float) -> void:
    if health <= 4:
        remove_child(segment)
    if health <= 5:
        add_child(segment)

This doesn’t solve the strangeness that is your two if statements, why would you want to remove the child then immediately add it within the same frame?

this was just to test if it worked and i wanted to know where it would reappear after i called it back