Invalid get Index 'value' (on base :'null instance') Help

Godot Version

Godot 4

Question

So I have an Area 2d and a character body2d and I want the progressbar(healthbar) to reduce its value when the area 2d touch the characterbody2d but then this error comming up. I cant search for it anywhere on youtube. Pls help!!

The error says what ever you are trying to get value from is null, it doesn’t exist. It will point to a line of code in one of your scripts. for example, if it points to this line then progress is null and you will have to find out how progress is defined and why it hasn’t been assigned or why it has been deleted.

progress.value = 100
extends Area2D
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var health_barr = $slime/HealthBarr2

var speed = 500
var bullet_direction
var is_touch: bool = false

func _ready():
	await get_tree().create_timer(0.5).timeout
	animated_sprite_2d.animation = "Splash"
	await get_tree().create_timer(0.15).timeout
	queue_free()
	health_barr = 50

func _process(delta):
	if is_touch:
		return
	position -= bullet_direction * speed * delta




@onready var slime = $slime

func _on_body_entered(body):
	if body.name == "slime":
		health_barr.value -= 10

	if body.name == "Ground":
		is_touch = true
		print("enter")
		animated_sprite_2d.animation = "Splash"
		await get_tree().create_timer(0.2).timeout
		queue_free()


i dont really understand so here is the code… the errror is on 28

You are freeing your object, then trying to use it.

Assuming it’s this line, let’s get thorough with that those two conditions I posted. I feel like a slime node could be deleted in the progress of the game, may be more useful to post the slime’s script.

  1. is health_barr defined correctly?

    Does your scene tree have a child named “slime” and does that have “HealthBarr2” under it?

  2. was health_barr deleted?

    Does the slime call queue_free()?

1 Like

In the slime script there is nothing that make the slime quere_free feom the game

Cool, what about no.1? Could you attach a screenshot of your scene tree?

1 Like

In the _ready() method you are calling queue_free(), which I believe is freeing your script’s parent from the tree. Try commenting that line out and see if the error persists.

nope i tried the error is still there

I don’t see an Area2D in this scene tree, and none of the script’s could possibly make use of $slime. Both the FarmHero and slime characters are instanced scenes, with added children. I would be willing to bet that the slime should have “HealthBarr2” in it’s slime scene, and the slime script should handle that health bar entirely in it’s own script, making use of a “take_damage” function to reduce health and the ui element at the same time.

yea the area2d is a bullet and its will spawn when i click the mouse

its will not work if I add the area2d into the tree

Let’s try removing any hard coded slime-paths from the script. You’ve got a good start using _on_body_entered and checking for the body’s name, I think you only need to make more use of body. The more slimes you add the less this will be able to work though, as body.name will not equal “slime2” or worse “@ slime22321@”

extends Area2D
@onready var animated_sprite_2d = $AnimatedSprite2D
#  @onready var health_barr = $slime/HealthBarr2
#  @onready var slime = $slime

func _on_body_entered(body):
	if body.name == "slime":
		# may be best to make a body.take_damage(10) function
		var health_barr = body.get_node("HealthBarr2")
		health_barr.value -= 10

	if body.name == "Ground":
		is_touch = true
		print("enter")
		animated_sprite_2d.animation = "Splash"
		await get_tree().create_timer(0.2).timeout
		queue_free()

Try adding this to your slime script:

extends CharacterBody2D
class_name Slime

@onready var health_barr = $HealthBarr2

func take_damage(amount: float) -> void:
    health_barr.value -= amount
    if health_barr.value <= 0:
        print("slime died!")

This way we can filter by class_name instead of through name, and make use of a take_damage function.

func _on_body_entered(body):
	if body is Slime:
		body.take_damage(10)
2 Likes

there is an error at " if body is slime" The error say " could not find type “slime” in the current scope"

It does care about capitalization, and make sure to save the slime script for it to be globally recognized as a class.

yea ik the error still comming up

Can you share your scripts?

extends CharacterBody2D

const speed = 100

@export var plr: Node2D 
@onready var nav_agent := $NavigationAgent2D as NavigationAgent2D
@onready var health_barr = $HealthBarr2



func _ready():
	health_barr.value = 10
func _physics_process(_delta: float) -> void:
	var dir = to_local(nav_agent.get_next_path_position()).normalized()
	velocity = dir * speed
	move_and_slide()
	
func makepath() -> void:
	nav_agent.target_position = plr.global_position

func _on_timer_timeout():
	makepath()

#Health Function
func take_hit(amount: float) -> void:
	health_barr.value -= amount
	print("get dmg")
	if health_barr <= 0:
		print("Slime Dead")
1 Like