+1 vote

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.

in Engine by

1 Answer

+2 votes
Best answer

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.

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 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 webmaster@godotengine.org with your username.