0 votes

How does that work in Godot? I cannot find a getComponent like Unity has....so, 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 (160 points)
edited by

try

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

Or

 func _on_Area2D_body_entered(body):
      body.func_name()

#

func_name():
  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 (650 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.