body.get_name not working

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

usually if body.name works to collide with other players but its give me the error “The identifier “body” isn’t declared in the current scope” it ususally works but its not.
if Input.is_action_pressed(“p1_push”):
if body.get_name() == “Player_1”:
body.hit_by_enemy()
can someone help

Where does body.get_name() work for you? I think one of the possibilities is that the “body” variable is exclusive to a collision function triggered via a signal (e.g. body_entered for Area2D). It would, in that case, work within body_entered(), but not the other functions.

Ox0zOwra | 2022-08-05 13:16

:bust_in_silhouette: Reply From: SteveSmith

As the error says, it’s because body doesn’t exist. If it sometimes works, you must be removing it at some point. I would also use groups instead of "getname() == “Player1”.