Triggering Animation from body_entered function - error?

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

I want to trigger an animation whenever my Area2D is entered by a player body. (It is used to change color modulation in a certain area).

extends Area2D

func _on_TRIGGERYELLO_body_entered(body):
	$Player/PlayerColorizer.play("PlayerYellow")

But whenever the body enters, it returns error.
Attempt to call function ‘play’ in base ‘null’ instance on a null instance.

Am I referring to Player wrongly? Why doesn’t it find it?

Is your Player under your Area2D node? if not…then probably not finding player

lowpolygon | 2020-01-10 09:23

No, it’s not under Area2D, it’s under World.
How do I properly address a node elsewhere?

verbaloid | 2020-01-10 11:23

Try get_tree().get_root().get_node(“player”)

edit: since I don’t know exactly where your area2d is, I am using get_tree().get_root() If your area2D is directly under root node, you can try get_parent().get_node(“Player”)

lowpolygon | 2020-01-10 11:32

I still can’t figure out what’s the right syntax to use these two statements together.

getparent().getnode("Player").
$Player/PlayerColorizer.play("PlayerYellow")

Should I first assign a var with getparent().getnode(“Player”)?

verbaloid | 2020-01-10 14:40

:bust_in_silhouette: Reply From: AiTechEye

Im pretty sure you are trying to get its neighbour, if so you need to use get_parent()

extends Area2D

func _on_TRIGGERYELLO_body_entered(body):
    get_parent().get_node("Player/PlayerColorizer").play("PlayerYellow")

you can use print_tree() to check if the node is under it

Yes, you are right! Indeed, that works! Thank you SO MUCH!!! It really helped!

Thanks everybody who commented and helped!!
You are awesome!

verbaloid | 2020-01-10 14:42

:bust_in_silhouette: Reply From: mohsen.ph.69

You have several problem
don’t use $player to access the player body instead use body and check if the body is not null and if the body is the player, for checking if body is player you can put player collision mask and area collision mask a same thing

extends Area2D

func _on_TRIGGERYELLO_body_entered(body):
     if body == null: return
    body.play("PlayerYellow"
:bust_in_silhouette: Reply From: lowpolygon

Assuming your player and area2D node IS directly under root / world node and your script is attached to area2d node

get_parent.get_node("Player/PlayerColorizer").play("playeryellow")

Assuming your area2d node is NOT directly under root/world node and script is attached to area2d node

get_tree().get_root().get_node("player/PlayerColorizer").play("playeryellow")

And you don’t need to assign a variable to any of the lines