This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

How does that work in Godot? I cannot find a getComponent like Unity, if a body is entering my area2d, the variable crosshair(variable from the colliding body) should be made visible.
This does not work however:

func _on_Area2D_body_entered(body):
   body.get_script().crosshair.visible = true

variable I try to access is

export (NodePath) var crosshair
in Engine by (173 points)
edited by


func _on_Area2D_body_entered(body):
    body.crosshair.visible = true


 func _on_Area2D_body_entered(body):


  crosshair.visible = !crosshair.visible

1 Answer

0 votes

get_script() returns just the Script resource, that contains it's text. For accessing node variables you need to use dot notation as ramazan showed.

func _on_Area2D_body_entered(body):
    body.crosshair.visible = true
by (654 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.