Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dangermousenz · Feb 27, 2013 at 08:24 AM · c#classes

How can I reference/import one C# script from another?

Umm, this time I'm going to start with some background.

I come from a Python background, and when working on a large project, you'll break it up into files, I often do one per major class, and import them back into my main file. For example:

 from assets.scripts.classes import controller, settings
 from assets.scripts.functions import logging
 
 class GameController():
     myController = controller.Controller()
     mySettings = settings.Settings()
     logging.log("Everything is fine and dandy")

Now, those import scripts are simply living on disk in /assets/scripts/classes/controller.py etc, and they get loaded at run or compile time.

Now, in Unity/C#, the only way I can see to replicate this behaviour, is to create dummy GameObjects in my scene, attach the scripts to them, and then access these GameObjects/scripts from my main script using

 Classes = GameObject.Find("Classes")
 myController = Classes.GetComponent(Controller).Controller()

But, is this really the "right" way to do it?

Any feedback on how you guys are writing scripts like this would be really fantastic

Thanks Dave

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Feb 27, 2013 at 08:28 AM

Well the classes are available from anywhere within the same namespace - so you don't need to import them as you do in Python.

What you need is a way of accessing the variables of those classes or the instances of the classes attached to another object. You have a few choices (Unity Gems has a number of articles we wrote on this).

You can:

  1. Add static singleton variables to your classes to access a singleton component (perhaps Controller is like this).

  2. Use GetComponent to access the components on another object (probably not for singletons)

  3. Use the inspector to assign references to the other components that exist between individual objects

There are other methods when you are coding for collisions etc, but those are the primaries.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tarlius · Feb 27, 2013 at 09:19 AM 0
Share

This ^

Using Find methods to get objects is slow, but is an option also. But the "correct" way is what @whydoidoit suggests. Note that GetComponent is relatively slow, so its a good idea to cache the result if its an object you will use a lot.

avatar image dangermousenz · Feb 27, 2013 at 09:31 AM 0
Share

Aah, that's awesome, thanks a lot whydoidoit!

Reading your answer, I think it's become more clear that I need to change my way of thinking, rather than replicating the behaviour from another language.

And that link looks fantastic, will be good for some weekend reading :)

And thanks Tarlius, I'll definitely be caching these :)

avatar image Tarlius · Feb 27, 2013 at 09:40 AM 0
Share

Another big deal about the unity way, is how it deals with Resources. Although again, you can use the traditional way if you prefer for most things.

The "Unity Way" tends to be the fastest way, and thats what Unity is mostly about. Letting us do annoying things more quickly so we can spend more time doing interesting things (game logic). But if you want to do more complicated things, Unity can usually accommodate that too (although the way is less well-trodded, so expect more headaches)

avatar image
2

Answer by Tarlius · Feb 27, 2013 at 09:26 AM

It is probably worth mentioning that you don't need to attach things to GameObjects though. You can use regular C# classes too, by not inheriting from MonoBehaviour (which will let you use constructers/etc as per "normal" C#).

I personally prefer to have "dummy" GameObjects in the scene that I can tweak visually, but it is an option. The main bonus of doing it the Unity way is that Unity will save all the public and Serialised values for you, which makes initialisation much easier, as well as all the editor extensions/inspector/etc.

Obviously, if you don't use MonoBehaviour you also won't benefit from/(be cursed by?) the Awake() Start() OnEnable() etc model, and you also will not be able to StartCoroutine without a reference to a MonoBehaviour to start one on (although you might be able to use threads for some behaviour, although a lot Unity functionality is not thread-safe which will limit what you can do)

Edit: All values was a lie. It will save the ones it knows how to Serialise, which is not "all". You can't do more complex datatypes, but Arrays (of supported types), instances of [Serialisable] custom classes, most primatives, and most structs (Vector2/3, etc) will work.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Where is the best place to host a class that is not intended to be a MonoBehaviour or Component? 1 Answer

Classes and Scripting 2 Answers

Calling a method of a class that is a part of another class? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges