Cannot call method 'get' on a null value

Godot Version

v4.3

Question

The error i’m recieving is:
Cannot call method ‘get’ on a null value.

on the last line of this code

I’m also recieving these errors:

So i’m assuming for some reason my code isn’'t recieving the animation tree and animation player nodes from my scene tree evcen though they are in it.
image
I’m new to coding, so is there anything I’m missing? Thanks

Hi ! I think it’s you who got the path between AnimationPlayer and AnimationTree wrong, you can try dragging the node into the code and pressing the ctrl key when you release it :grin:
This will get the exact path to the node, and you should replace lines 22 and 23 of the code :kissing:
As for you saying “Cannot call method ‘get’ on a null value.”, it is also due to the wrong path in the above two lines of code, and the two variables are assigned null, just follow the steps above to change it :stuck_out_tongue_closed_eyes:
If the node is not in the scene, instantiate the node first :kissing: :kissing:
I don’t speak English well, it’s machine translated :slightly_smiling_face:

Hello! Thanks for your reply, However when I tried dragging the node into the code and pressing ctrl when releasing, it is still turning up with the same issue. How do i instatiate the node? Thanks.

It says relative to “/root/Player” this script may be accidentally attacthed to the wrong node or a Autoload/Global

the cannot call method get on a null value error is an order of operations error, @onready doesn’t specify which variables should be loaded first, you should convert these variables to use the _ready() function like so

var animation_tree: AnimationTree
var state_machine: AnimationNodeStateMachinePlayback

func _ready() -> void:
    animation_tree = $AnimationTree
    state_machine = animation_tree.get("parameters/playback")

Hello! Thanks for the reply!
I tried implementing what you suggested, however it still comes up with the same issue

This script is currently attached to the villager node, the parent node fo the animation tree and the animation player. What is an Autoload/Global and how do I disable it?

I also have a question, what happens to the animation_player it isnt declared in the code you sent. Also how do you insert code in a grey text box like you just did.

Pasting code is a good idea, ctrl+c to copy; ctrl+v to paste. Doing so between three ticks formats it like this

```
type or paste code here
```

type or paste code here

Global is in the Project settings, the “Globals” tab. I figure you wouldn’t want one for “player.gd” if it is there.

I know it’s not the best answer but you could also try a separate project with the same setup or copy and pasting this one to a test folder. With that done you could try doing something simple as a test like $AnimationPlayer.play(“insert anim”) and see if it comes back at all.

Hello, i think the reason is because its a global variable, but I want to take variables from this code and use it in another script:

func _physics_process(delta):
	if player_indetection_zone == true:
		position += (Player.player_position - position)/speed
		if (Player.player_position_x - position.x) < 0:
			$AnimatedSprite2D.flip_h = true
		elif (Player.player_position_x - position.x) > 0:
			$AnimatedSprite2D.flip_h = false

when I am coding my enemy.

Is it possible to fix my problem without disabling it as a global script?

Thanks for the reply.

Thanks for the response,
I think I know what was the issue. If you can provide anmy knowledge with what i said in my reply gertkeno’s comment, it would be much appreciated.

A global isn’t the right way to go about a player. Often player stats, like max health and currency are stored in a separate global, but not the whole player and/or their position.

You could use groups to find the player, with get_tree().get_first_node_in_group

I would bet since you have a player_indetection_zone variable that you have an area to detect the player. If so you can store the body entered since it is the player.

var target: Node2D
func _on_detection_zone_body_entered(body: Node2D) -> void:
    if body.name == "Player":
        target = body


# Using target instead of Player
func _physics_process(delta):
	if target != null:
		position += (target.position - position)/speed
		if (target.position.x - position.x) < 0:
			$AnimatedSprite2D.flip_h = true
		elif (target.position.x - position.x) > 0:
			$AnimatedSprite2D.flip_h = false

It works :smile:
Thank you so much, as a newbie I really appreciate the help you provided.
:pray:

1 Like

Hey there, glad someone with more experience was able to help you. Sorry I wasn’t able to get back sooner. Keep chipping at it, I still to this day consider myself new.