Attempt to call function 'subtract_life' in base 'null instance' on a null instance

Godot Version

Version 4.3 stable official

Question

` I have a node called “GameManager” with a GDscript called “game_manager” that contains the function “subtract_life” as below:-

When I attempt to call the “subtract_life” function from a script called “killzone” I randomly get the error message “Attempt to call function ‘subtract_life’ in base ‘null instance’ on a null instance.”

Here is the code for the “killzone” script:-

I also tried to code the “killzone” script using a reference to the “GameManager” node as below, but the result was the same as seen below:-

Bear in mind that this appears to be totally random and the “killzone” script can work maybe once, twice or three times, but will then crash on the first “killzone” another time!

Any help greatly appreciated.

This doesn’t work because scene unique nodes has the same scene limitation (see more: Scene Unique Nodes — Godot Engine (stable) documentation in English)

Try to use groups instead:

# In game_manager.gd

func _ready() -> void:
	# Add this node to "game_manager" group
	add_to_group("game_manager")
# In killzone script
func _on_body_entered(body: Node2D) -> void:
	# This will call the function "subtract_life" in all nodes from 
	# "game_manager" group
	get_tree().call_group("game_manager", "subtract_life")
1 Like

Perfect! Thanks for the help. Now I’ll read the docs to find out more about Scene Unique Nodes and groups. :+1:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.