[C#] How do I get the script of another node?

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

In Unity I can do this:

SomeScript s = someObject.GetComponent<SomeScript> ();

What is the equivalent in Godot? I’ve tried

SomeScript s = someObject.GetScript() as SomeScript;

But this doesn’t work. GetScript() returns a Reference which I’m not sure what to do with. I just want to get the script attached to another node which I have a reference to.

:bust_in_silhouette: Reply From: anonymous

While writing the question I managed to figure it out. Simply put, the object IS the script! You can simply take the object and change it into the script:

SomeScript s = someObject as SomeScript;

Coming from Unity, this does seem strange. But it makes sense, if you consider that Godot always has one script per node, this is simpler.

1 Like