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 /
This question was closed Mar 23, 2014 at 05:25 AM by getyour411 for the following reason:

Too subjective and argumentative

avatar image
0
Question by jptoniolo · Mar 23, 2014 at 05:11 AM · c#designoop

Question on Unity OOD best practices

I apologize in advance if this is a dumb question, but I have some design level questions about scripting in Unity. I am a computer science student, and I am used to working with objects and their relationships, but I am a little confused on the best practice for unity (specially c# scripting). For instance, if I were programming a game in java I would have a Player class which HAS a position, HAS hitpoints, HAS speed, etc. This is all standard class composition. I get a little tripped up though with Unity's format, as it seems to be that I can take two different directions. 1. A monobehaviour deriving Player class, which has other information like hitpoints and speed. 2. 2. A non-monobehaviour Player class with standard constructors and such, which HAS a GameObject member which is it's geometry/animation/etc.

With (1), we have access to Monobehaviour's vast array of methods and functionality, but in order to set up our Player we'd need to either use public variables or some sort of factory method constructor since void Start() takes no arguments, ie.

 public static PlayerObject getPlayerObj(param1, param2, param3, ..., paramN)
 {
    GameObject go = Instantiate(somePrefab) as GameObject;
    PlayerObject po = go.getComponent<PlayerObject>();
 
    po.p1 = param1;
    ...
    return po;
 }

For (2), we can use a constructor and do not need a factory method or "Inspector public variables" to initialize. We can also use MonoBehaviour.Instantiate to setup our gameobject member.

In both cases, we have a gameObject member which references the game object the player represents. The difference is in the initializing and also the ability to manage game objects. That is, in order to update, we need something attached to an object, not an object attached to a script. Sure we can have a "GameManager" mono script on an empty object which manually updates players but this seems... not as intended. It seems like the correct choice here is (1), but it's just different than Im used to in say java or c++ where the Player class would HAVE geometry and positional information.

I guess I just want to know what is the best way to most effectively use Unity's unique style of scripting-gameobject fusion to make games that are more complex than dropping a few values for public variables in the inspector? Is it Monobehaviours (which have update functions and can handle themselves) with other data initialized by factories, or non-Monobehaviours with gameobject members which need to manually update since there is no Monobehaviour script attached to that gameObject member (the gameobject is "attached" to the script instead, thus some higher Mono script would have to loop them in IT'S update function).

Thanks very much for any input, just trying to avoid the pain of doing this the wrong way.

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 getyour411 · Mar 23, 2014 at 05:25 AM 0
Share

Your post is nicely thought out and I hesitate to close it, but it's subjective. You'll get a few #InheritanceSucks, you'll get a few use $$anonymous$$onos for all, and you'll get a few 'both have merits so use the pattern that's the best fit' (that's what I'd say, although I'm not much of a programmer).

Please copy and paste this over to Unity Forums where open-ended discussions like this are frequent.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

20 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Best practice for adding specific attributes to an object but not all of them 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how to planning n designing game structure/architecture? 0 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