How do you fix an "Invalid get index 'value' (on base: 'null instance')" debug return?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sousr

How do you fix an “Invalid get index ‘value’ (on base: ‘null instance’)” debug return in the code:

func _on_Area2D_body_entered(body): #connected signal
    if body.get_name() == "Character":
        body.respawn()
        $HealthBar.value -= 15 

The $HealthBar is a TextureProgress node and I am trying to decrease its value when the main character of my game enters the enemy’s area2d. I’m not sure why this code keeps returning as a null instance.
Thank you for helping

:bust_in_silhouette: Reply From: klaas

Hi,
$HealthBar is not accessable from this script.
$HealthBar must be a direct child of the node with this script to access it like so.

Oh ok, thank you! So, how would you code the change in value for my HealthBar from the other script (it is on the basis that the character enters the enemy’s area2d and hence loses some of its health). Thank you again!

sousr | 2020-08-31 08:48

read about nodePath’s here

https://kidscancode.org/godot_recipes/basics/getting_nodes/

you could use something like get_node(“/root/baseNodeName/myProgressBar”)

klaas | 2020-08-31 08:59

Thank you for your help!

sousr | 2020-08-31 09:07