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
1
Question by SisterKy · Dec 26, 2009 at 12:48 AM · scriptingbasicsoop

Fundamental difference between Class and Object ?

I thought "classes" are kind of the "blueprints" for "objects".
But there's a class called "Object", so when we talk about "Object" it could mean "Blueprint of an Object" and "an Object itself" at the same time?? (Freaky!! O_o )

For example:
The "AnimationCurve"-Class does not derive from the "Object"-Class.
It has a Constructor which creates "an Animation Curve".
Is that newly created Animation Curve not an Object since the "AnimationCurve"-Class does not derive from the "Object"-Class?
Or is it an Object, but of the Class "AnimationCurve" (and not of the Class "Object")?
Or is it somehow an Object of the Class "Object" anyway? (If so, is that the way a Constructor works? Turning a non-Object-Class into an Object-Class-Object...? o_O )

So is there a difference between "Object" when talking about
the ObjectOrientedProgrammingConcept of "Object" and
the "Object"-Class defined in the API?

__

"Of the Class XY" is my next problem... When do you say an Object is "of some-or-another class"?
Does something necessarily need to be constructed from a Class to be "of Class XY"?
And/Or is a GameObject "of Class XY" once it has the Script which contains "Class XY" attatched to it?
Or is it somehow enough to have all the Attributes and Abilitys of Class XY to be "of Class XY"?

__

(also see http://answers.unity3d.com/questions/1955/not-every-class-in-the-api-is-derived-from-the-object-class-so-what-are-they for Jashans excellent answer on my very closely related topic that got me to ask this slightly different question in the first place)

Thanks & Greetz, Ky.

Comment
Add comment · Show 1
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 jashan · Dec 26, 2009 at 09:40 AM 0
Share

Your original understanding that classes are "blueprints" for objects was actually quite right. See my answer to "Not every class in the api is derived from the object class so what are they" for a better understanding ;-)

2 Replies

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

Answer by Lucas Meijer 1 · Dec 26, 2009 at 01:10 PM

Your understandable confusion indeed comes from the fact that "object" in terms of object oriented program, has a different meaning than the unity api specific type called UnityEngine.Object.

I usually prefer to not talk about 'objects', but instead refer to them as 'instances', so things get less confusing when you actually want to talk about UnityEngine.Object.

Regarding your second question, Unity's design philosphy is based around "has a" instead of "is a". In other game engines, you might find a class hierarchy like this:

EnemySoldier -> Soldier -> Humanoid -> Unit -> MovableThing -> BaseObject.
(-> indicating an "inherits from" relationship)

A lot of people these days think that having deep "enheritence chains" like that are usually not the best solution to the problem at hand. Instead a lot of people, including the unity engine, are using a more component based approach:

GameObject has:

  • EnemyBehaviour
  • SoldierBehaviour
  • MovableThingBehaviour

It's a "has a" relationship, instead of a "is a" relationship. It makes it easier to shuffle things around while you're developing. One thing to note is that nothing is stopping you from mixing the two. For instance if you have 3 different MonoBehavouir classes, that share a lot of code, you can use enheritence and put that shared code into a base class.

There's a large amount written on this topic, if you google for "composition versus inheritance" you'll find plenty for several rainy sundays.

Comment
Add comment · Show 1 · 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 26, 2009 at 08:19 PM 0
Share

Thank you! So I'm not plain stupid, yay!! =D The "has a" thing is an insightful point and I'm recently beginning to really see the merits of composition over pure inheritance... however, I still see the "is a"-term used a lot on the forums as well as in the tutorials (if I'm not mistaken) when it's a script of that class attatched to a GameObject... so can I assume that this is often due to blurry use of ter$$anonymous$$ology?

avatar image
2

Answer by Mortim · Dec 26, 2009 at 01:26 AM

A class is a template to create objects (or instances) of that class.

A class can inherit from another class, which means that instances from the former class are also instances from the latter.

For example, the class "Cat" may inherit from a class "FurryCreature". If "Igor" is an instance from the class "Cat", then "Igor" is also an instance from the class "FurryCreature".

In most object-oriented programming languages, you will find a particular class named "Object", from which all classes inherit. Therefore, every object is an instance of the class "Object".

In unity, in order for an object to be "of class XY", the object needs to have the "XY" script or component attached to it, or needs to have been explicitly instantiated in some script.

Object-oriented programming is a deep and rich field, which builds itself on basic but very abstract concepts. In order not to get lost, a good entry point is almost mandatory, otherwise it is easy to mix up concepts and get confused. I suggest the wikipedia article on POO, which is lenghty but covers the subject :

http://en.wikipedia.org/wiki/Object-oriented_programming

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 SisterKy · Dec 26, 2009 at 09:06 PM 0
Share

"you will find a particular class named "Object", from which all classes inherit. Therefore, every object is an instance of the class "Object"." --- Yes, that had been my understanding. But Unity appeared to not stick with that, wich was my problem... (see also my other question in the link above) --- As I'm lucky to have a Java-Programmer at my hand to explain basic OOP-concepts to me, I flinched from reading that extensive Wiki-article. But having looked into it now, I have to admit it's quite understandable and newbe-friendly... thank you for making me read it...

avatar image SisterKy · Dec 30, 2009 at 08:24 PM 0
Share

Wait a moment... After learning about this some more I have to dig this up again: I had been under the impression, that Constructors always build Objects. But Structs never inherit from anything & also have Constructors = not all Objects can be instances of the class "Object"?? :-/

avatar image SisterKy · Dec 31, 2009 at 02:56 PM 0
Share

Yes, Bampf's answer http://answers.unity3d.com/questions/2077/does-a-constructor-always-build-an-object affirms my guesswork...

avatar image Mortim · Dec 31, 2009 at 04:33 PM 0
Share

An object is an instance of a class. There is no need for its class to inherit from the "Object" class for its instances to be called objects. The fact that there is an general class called "Object" is what you might call a reification of the object concept in the API. But such reification is not needed for the concept itself to exist.

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

Not every Class in the API is derived from the "Object"-Class, so what are they? 2 Answers

What's the difference between a struct and a class? 1 Answer

Does Unity support multiple Inheritance? 3 Answers

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

Does Unity support the declaration of several classes in one script? 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