0 votes

Im making a game 2d and this i have this error in my health script:

Attempt to call function 'set_value' in base 'null instance' on a null instance.

get_node: (Node not found: "player1/HealthBar_p1" (relative to "/root/Player1Script").)
Error de C++: Method failed. Returning: nullptr
Fuente C++: scene/main/node.cpp:1465 @ get_node()
Rastreo de Pila: player_1_script.gd:34 @ _physics_process()

This is the code:

extends KinematicBody2D

var health = 100

func _ready():
    pass

func _physics_process(delta):
    get_node("player1/HealthBar_p1").set_value(health)

I copied the path by right clicking on the node
Image of the nodes:
.

can anybody help me?

Godot version v3.5.1.stable.official [6fed1ffa3]
in Engine by (12 points)
edited by

It was the same error
Before

#Code
get_node("player1/HealthBar_p1").set_value(health)

#Error
get_node: (Node not found: "player1/HealthBar_p1" (relative to "/root/Player1Script").)

After

#Code
get_node("HealthBar_p1").set_value(health)
#Error
get_node: (Node not found: "HealthBar_p1" (relative to "/root/Player1Script").)

Hmm sorry I have created a little test program and just cannot recreate your issue. Can you please try something else for me?

try adding this at the top of your script

onready var HealthCapture = get_node("HealthBar_p1")

and then where you are trying to change the variable in the script try this instead

HealthCapture.health = 1

I would just like to see if you get the same error? Obviously change the 1 to whatever makes sense in your script.

Oh sorry also add a script to healthbar and add a variable health as well or the above wont change anything. If this works you can add something like this into the script on healthbar

var health = 100

func _process(delta):
    self.value = health

Thanks for the help but i am not trying to change a variable, i want the value of a variable to control the percentage of the progress bar

Yes I appreciate that, the second script on the healthbar in my second message would make the change. But thats okay if you dont want to make the changes. I am not sure what is causing it though as I was unable to replicate your issue.

1 Answer

0 votes

Actually I have been reading your messages and I think the problem may be that you are looking at the wrong part of your code. You said you tried;

get_node("HealthBar_p1").set_value(health)

and you originally copied the line

get_node("player1/HealthBar_p1").set_value(health)

but the error message says

getnode: (Node not found: "player1/ProgressBar" (relative to "/root/Player1Script")

so you are trying to fix the wrong part of your code. Its the progress bar not the health bar. Did you delete that node perhaps?

by (3,321 points)

So sorry. I miscopied the error, it actually says HealthBar_p1 instead of ProgressBar, now I've edited the original post.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.