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 /
  • Help Room /
avatar image
0
Question by sxzhyun · Jul 15, 2016 at 05:37 AM · c#oop

OOP Conceptualization in Unity

Hi - I'm a newcomer to Unity, and I'm having trouble understanding how Object-Oriented Programming works here. I come with some experience of Java, so I largely understand what OOP is - or so I think.

I always understood that OOP centered around objects that were instantiated from classes and their interactions between one another. But here, that seems not to be the case - GameObjects are modeled by classes themselves, not instances of them. Then what is the purpose of constructors and instantiation from classes, and etc?

I apologize if I sound dumb, but I'm a little confused. Could someone help me?

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
0

Answer by jdean300 · Jul 15, 2016 at 05:55 AM

A common mistake with OOP is to think that inheritance hierarchies are the way to accomplish everything. That is the commonly taught and explained version of OOP because it is simple and gets the salient points across. However, as things become more complicated, you should begin favoring composition over inheritance - which is EXACTLY how Unity works. You add components to a GameObject in order to define it's behaviours. There is nothing about this that is not OOP. You can still use inheritance within the components, but all a GameObject represents is a container for components.

Constructors have nothing to do with OOP - they are a byproduct of language design. It so happens that certain OOP concepts can be easily implemented with the help of constructors, but they are not needed. For anything that extends MonoBehaviour - Unity takes control of the constructor. You should not call the constructors yourself. However, you can still do any initialization you need through the Awake/Start/OnEnable functions. You do not need access to the constructor.

Unity can use OOP just as much as Java or any other programming language - it is just modeled in a slightly different way. Unity pushes you towards composition, which is generally preferred over inheritance, and is still a well used concept in OOP design. Furthermore, composition works VERY well for games. With games you very often run into "diamond inheritance" situations, such as:

       Enemy
         /   \
    Mage     Fighter
         \    /
      Battle-Mage

This seems to require multiple inheritance, which is prohibited in most OOP designs for a plethora reasons. Composition can model this very easily - give a GameObject an Mage and Fighter components and now it is a Battle-Mage.

Remember that OOP is a concept to help you reason about code - it is not a necessity. Sometimes, OOP makes a mess of code that could be very easily modeled in a different way. If you write some code that does not follow OOP conventions, just make sure you are doing so for a clear reason. Have a clear design - whether it's OOP, functional, procedural.... it does not matter.

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

191 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Information about Classes and Instantiate Objects C# 2 Answers

IAP function not calling 0 Answers

Cyclist script bug? 0 Answers

Building Scripts on each other 1 Answer

How to inherit from other class in c#? 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