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
0
Question by TornadeVent · Aug 06, 2018 at 05:30 PM · characterclasslogic

Should I create a Main Character Class or just a Script?

Hey, I'm kind of new with Unity so, I'm not very familiarized with certain things. One of my biggest doubts is, should I create a Main Character class or just a script for him?

Like, I'm making a game that the player will only be able to play with just one character (the main one) and he doesn't have many stats. So it doesn't make much sense for me to create a class which won't have much going on inside, but, maybe, for practical reasons, I should create it? Any thoughts?

Thanks for the help! Sorry for the writing, english isn't my best language.

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

2 Replies

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

Answer by JVene · Aug 06, 2018 at 07:11 PM

Students of Unity generally come to think of scripts as a programming concept, but it isn't. The original meaning of the word script is just a text file, and their original purpose was to customize the behavior of an application. Unity uses the word script, so it sticks, but in all cases the primary programming concept of C# is the class, not a script. When Unity creates a script from within the editor, it is a class. The name of the class is also the name of the script file, which is a convention of C# and other languages, but that doesn't mean the script is a programming concept by itself, it really isn't.


You'll notice, then, as a result of this, that in your question when you ask if you should "just [create] a script", in reality that is a class. It is derived from MonoBehaviour, and appears as a component attached to a GameObject. From there you can create additional classes where it is appropriate to do so. The focus of object oriented programming in languages like C# is to determine what concepts are being represented. We focus on what classes should be doing particular jobs. Vector3 itself is a good example. Without Vector3 we would have to perform all of the math associated with vectors based on knowledge of linear algebra using what just happens to be the appropriate 3 variables, namely x, y and z. Vector3 wraps the concepts of rotating by quaternions, finding lengths or distances, or translating vectors (and a lot more) into the Vector3 class, so you don't actually have to do the linear algebra to accomplish the calculations. The class does this for you.


When you ask "for practical reasons", that's a good bit of thinking. What are those reasons? If you don't have them in mind at the moment, a class is not yet indicated. However, think about Vector3 again for a moment. What you would discover if you did not have Vector3 is that you would find code repeating a number of calculations, over and over, in the code. You'd see code repeated frequently, doing the same thing in multiple locations in your code. That is a hint that an object should exist, but has not yet been written.


At first, the script attached to a GameObject, which is a class remember, may be the very class that should perform the character behavior (and storage of character data) required in your design. At some point, though, you might notice a tendency for something to be repeated. It could be as simple as a bunch of "if/elseif/elseif" tests that seem to repeat in Update. This may hint to an object that doesn't yet exist.


Update tends to become a mess for student programmers. Sometimes repeated code merely indicates a concept should be wrapped up into a method (a function). A method can be a good way to wrap up a single, simple concept that is repeated. An example of this can be where "if" tends to test the state of two or three bools, over and over with different pattern. If "has weapon" and "weapon is ready" and "health > 10" then....with alternative "else if" for various permutations of the 3 bool pattern. It may be better to make functions that do these tests, named for the purpose of the test. If ( readyToFire()==true) is clearer than the 3 bool test required to determine this. Trust the compiler to make the functions fast (it can inline such a test, but the code is easier to comprehend).


On the other hand, sometimes member variables require more robust "management" to handle correctly. A position, represented by a Vector3, is exactly this kind of thing. So is the Quaternion representing a rotation, which is why Unity created a Transform, combining these two. It is a good example to follow.


Update itself is a very generic concept. It is a slice of time in which various actions are performed for that slice of time. FixedUpdate is a physics engine associated counterpart to it. These two methods represent the cyclic operation of the game engine. Filling update with a bunch of input tests and analysis makes it messy. Breaking those into generic steps makes Update more sane and readable, while possibly separating tasks into methods that make each one clearer.


At some point, though, when something appears repeated, and represents a higher level concept, it requires a class instead of just a method. Reading up on the general concepts behind object oriented programming in C# is, as a result, a required objective of any Unity student.

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 TornadeVent · Aug 06, 2018 at 09:15 PM 0
Share

Wow, thanks a lot! Everything seems so much clearer to me now! Yeah, and I think I'll just stick with the $$anonymous$$onoBehaviour class. I really don't see the reason why I should create an additional one... (for now). I guess I should study a with this word in $$anonymous$$d now, thank you!

avatar image
0

Answer by Strixie13 · Aug 06, 2018 at 06:50 PM

In Unity scripts are treated as classes. So make a script.

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

128 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

Related Questions

how can you make a wrapper class for a character? 1 Answer

Simplest way for some stat presets for RPG classes? 1 Answer

Making a Trait Mechanic? 1 Answer

Help with Character System wall climbing 0 Answers

Transform not a child ?? 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