Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Albert · Apr 19, 2010 at 05:22 AM · classuse

When do we need to use Class?

Hello everyone :)

I read some good amount of docs about class and its use with Unity, its importance, like inheritance and the heirarchy, but at which point do we really need to use it? I am making my first game right now, and i'm really new to this.

Let's say i have a Class of Plant, i have three different plants right now, what i am doing right now is i have all three plants in their own prefab, each with their own Script(the JS file). so when is the good time to use it?

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
8
Best Answer

Answer by Extrakun · Apr 19, 2010 at 06:39 AM

Take note that every time you write JS script, it is automatically a class, which is derived from MonoBehaviour, and have the same class name as the name of the file (and name of the behaviour), so you are always using classes.

You define your own classes manually, like this

class FooBar {
}

You define your own class explictly when you are sure you don't need Update(), Awake() and such functions (all behaviours' Update() is invoked once per game logic update, Awake() is called when the game first began and so on). I usually defined my classes manually when they are not game objects - for example, consider the Vector3 class. It does not need to be updated by the game constantly; so use a manual classes for data structures, game information and etc.

About your question, of designing the class Plant, it is good to keep in mind Unity3D is designed along a composite pattern. Instead of inheriting, you put different classes together to give each game object an unique behaviour. So don't think of having a 'plant' class, think along the line 'how can i break this plant down into discrete, reusable components?'

For example, I have three spaceships, one is a supply vessel, one a missile frigate and another a battleship with a massive cannon. What is common to those objects? A movement component/script. There should also be a missile component for firing missiles, and one for cannon firing.

So to put together the missile frigate, I take the basic movement script (class or component, it's just terminology), and the missile firing script, and put them together. For the battleship, I put the cannon and the basic movement together.

The power of this is that if the next time I want an awesome battleship-missile-firing supership, I just put the missile and cannon component, and code a battleship-firing component, and put together on to the same ship.

Comment
Add comment · Show 4 · 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 Albert · Apr 19, 2010 at 06:57 AM 0
Share

so basically, the use of Class in Unity are mostly for attributes/informations of an gameObject? that can be shared with related gameObjects? and seldomly change?

avatar image Cyclops · Apr 19, 2010 at 04:26 PM 0
Share

@Extrakun - a supership that fires battleships? Cool, I want one :)

avatar image Extrakun · Apr 19, 2010 at 05:32 PM 0
Share

@Albert, manually-typed classes is a must if you are using C#. So technically, behaviour scripts (with Update(), Awake()) etc. are also classes, just you don't have to manually explictly states so.

Yes, you are right in the sense that manually-defined classes in JS is best used as data structures and helper classes (example, parsing X$$anonymous$$L) and to encapsulate reusable code.

avatar image mangoblue · Mar 21, 2012 at 11:54 PM 0
Share

"Take note that every time you write JS script, it is automatically a class, which is derived from $$anonymous$$onoBehaviour, and have the same class name as the name of the file (and name of the behaviour), so you are always using classes."

Your comment gave me an "ah-ha!" moment.. where something about Classes finally clicked a little...

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

C# code to Java code 1 Answer

How to toggle a key for a car to go forward or backward? 1 Answer

Error whit c# code 'UnityEngine.Transform.position' because it is not a variable. 1 Answer

turn "is trigger" button off? 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