To encourage code reuse- i want to import methods and classes defined in my core unityscripts to be imported into other unityscripts. How do i go about this?

Note: An answer here seems too complicated for such a simple thing (i come from Python).

  • 2
    \$\begingroup\$ Note that Unityscript is not Javascript. Searching for Javascript solutions might not be useful. \$\endgroup\$ – MichaelHouse Feb 17 '15 at 19:40
  • \$\begingroup\$ Specifically he mean's that link has nothing to do with UnityScript/JavaScript-within-Unity. \$\endgroup\$ – jhocking Feb 17 '15 at 22:23

You can use inheritance for this. Have the methods and classes in a common base class that's extended by your classes that share this functionality.

Or you can use composition for this. Have the methods and classes in non-monobehaviour scripts. These will be instantiated as objects in your scripts that share this functionality.

Or you can keep these scripts contained in their own scripts at attach them as components to the objects where you want to use their functionality.

You have lots of options here, personally I'd go the inheritance route, but it depends on the kind of functionality you're sharing between scripts.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.