Accessing a variable from a colliding object?

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

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

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

ramazan | 2022-07-28 14:04

:bust_in_silhouette: Reply From: USBashka

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