attempt to call function "update_boss" in base 'null instance' on a null instance. Can anyone help me out with this?

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

I am currently trying to get the boss on my game to take damage from my character’s blast and lose lives. Here is part of the script for the character’s blast:

func _physics_process(delta):
translate(velocity)
velocity.x = SPEED * delta * direction
$AnimatedSprite.play(“shoot”)
var bodies = get_overlapping_bodies()
for b in bodies:
if b.is_in_group(“Enemy”):
b.queue_free()
for a in bodies:
if a.name == “BossEye”:
Game.update_boss(-1) This part right here is the problem
queue_free()

Here is the part of the script for the boss for bosses’ hearts:

onready var life = $“bosslives/Lives”
onready var Heartss = [$“bosslives/Lives/Sprite”, “bosslives/Lives/Sprite2”, “bosslives/Lives/Sprite3”, “bosslives/Lives/Sprite4”, “bosslives/Lives/Sprite5”]

var Boss_heart_full = load(“res://Assets/bob.png.png”)
var Boss_heart_half = load(“res://Assets/bob2.png.png”)
var Boss_heart_empty = load(“res://Assets/bob3.png.png”)

var livess = 10

func update_boss(delta):
livess += delta
for a in range(len(Heartss)):
Heartss[a].texture = Boss_heart_full
if livess < (a+1)2:
Heartss[a].texture = Boss_heart_empty
if livess == (a
2)+1:
Heartss[a].texture = Boss_heart_half
if livess == 0:
get_tree().change_scene(“res://Scenes/WinScreen.tscn”)

:bust_in_silhouette: Reply From: kidscancode

This is telling you that Game is a null instance. This means that wherever you’ve assigned the value of Game it’s not a valid node. This is a very common error and is typically the result of using the wrong node path.

Also, I’d point out that using get_overlapping_bodies() is probably the wrong approach here - Area’s signals are almost always a better solution. In addition, even if you’re calling that for the array of overlapping bodies, it’s not a good idea to loop through that array twice in a row.

This is the connection for the Game:

onready var Game = get_node(“/root/Boss”)

The main node on the scene for our level is a 2D node

Can you by any chance help me write it the code to fix this problem? I am very new to Godot.

ItsYoBoi | 2019-12-07 14:38

The code that I first posted first originated from the code for the main character’s health. This main character’s health is working but bosses’ health is not.

ItsYoBoi | 2019-12-07 14:43

Your problem probably stems from the fact that “Boss” is not yet ready when you’re calling get_node() on it. Nodes in the tree become ready in a particular order. If you want to be sure that a node is ready when you get a reference to it, either use a signal, or manage the references from a common parent, which will always be ready after all its children.

kidscancode | 2019-12-07 17:28

The script wasnt the problem at all. So on my boss level, I have a canvas layer, and within the canvas layer is two nodes (one for color rect and one for the lives). This is for the players lives and stay on the screen even if the player moves. I had a similar thing for the boss but without a canvas layer. I just deleted it and remade it. I got it to work. Thanks for the help!

ItsYoBoi | 2019-12-07 19:59

Actually I lied sorry. So this part of the code is for the player’s hearts:

func update_lives(delta):
	lives += delta
	for h in range(len(Hearts)):
		Hearts[h].texture = heart_full
		if lives < (h+1)*2:
			Hearts[h].texture = heart_empty
		if lives == (h*2)+1:
			Hearts[h].texture = heart_half
	if lives == 0:
		get_tree().change_scene("res://Scenes/YouLose.tscn")

This part of the code is for the bosses’ hearts:

func update_boss(delta):
	lives2 += delta
	for h in range(len(Hearts2)):
		Hearts2[h].texture = heart_full
		if lives2 < (h+1)*2:
			Hearts2[h].texture = heart_empty
		if lives2 == (h*2)+1:
			Hearts2[h].texture = heart_half
	if lives2 == 0:
		get_tree().change_scene("res://Scenes/WinScreen.tscn")

The player only has three hearts, and I was trying to give the boss fives hearts. I think this part of the code is the problem:

if lives < (h+1)2:
if lives == (h
2)+1:

ItsYoBoi | 2019-12-07 20:15