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
3
Question by SisterKy · Dec 30, 2009 at 09:20 PM · optimizationscriptingbasics

The Merits and Pitfalls of declaring multiple Classes in one Script?

It is my understanding, that if you attatch a Script derived from MonoBehaviour to a GameObject, only one Class (the one with the same Name as the Script itself) can be added to the GameObject... What exactly will be the consequences of this?

I assume you will not be able to find the GameObject, when using FindObjectOfType with one of the non-scriptname-classes?
Will you be able to get the non-scriptname-classes with GetComponent?
What else will fail?

How will you access the other Classes?

What about Scripts derived from other classes? (especially "ScriptableObject"?)

So far I fail to see any merits of declaring multiple Classes in one Script... except maybe not having a gazillion mini-Scripts lying around in the ProjectPane... Doesn't seem to be in any relation to the (assumed) down-sides... What am I missing?

Thanks & Greetz, Ky.

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

1 Reply

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

Answer by duck · Dec 30, 2009 at 10:18 PM

It only really makes sense to declare one MonoBehaviour class in a script file. However you may have cause to declare other 'helper' classes which are used by instances of your monobehaviour class.

For example, you may have enemies in your game, and those enemies might have designated targets. Now, you might initially decide that the 'target' of each enemy could simply be a reference to the target gameobject or transform, but suppose you want to contain more data in a 'target' object, such as 'priority', 'last fired at', 'last seen position', etc.

In this case you might decide to declare 'target' as a class in its own right, but since this is only ever instantiated and used by the 'enemy' object, it might make sense to include this class declaration in the enemy script file.

Ultimately this comes down to aesthetics - there's nothing stopping you from creating a separate script file for your 'target' class, even though it doesn't derive from MonoBehaviour.

Comment
Add comment · Show 2 · 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 SisterKy · Dec 31, 2009 at 02:39 PM 1
Share

Thank you for your answer =) But still, how will you access these classes? They can't be "static", otherwise all "Enemies" would have the same "Target"-Information, wich makes little sense. (And I thought classes aren't "static" anyway; only functions or variables are. :-/ ) But they aren't "properly" attatched to a GameObject, nor are they Objects in their own right, or are they? Hrrmm -.- I guess my understanding of the "class"-concept ist still somewhat vague...

avatar image duck ♦♦ · Dec 31, 2009 at 06:19 PM 0
Share

Just instantiate instances of them with the 'new' command. For example, if you have a helper class called "Target", you could use: Target thisTarget = new Target();

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to display data in an array 1 Answer

Accessing Variables within another gameobject's script 2 Answers

Playing an Animation that is within a GameObject 1 Answer

Optimizing Headless Servers 2 Answers


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