0 votes

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?

in Engine by (53 points)

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

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

Try gettree().getroot().get_node("player")

edit: since I don't know exactly where your area2d is, I am using gettree().getroot() If your area2D is directly under root node, you can try getparent().getnode("Player")

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")?

3 Answers

0 votes

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

by (200 points)

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

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

0 votes

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"
by (49 points)
0 votes

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

by (177 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.