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 vittu1994 · May 28, 2016 at 07:43 AM · classenuminstancestypes

idea for a script of a gameobject with different types/roles

Hey i have about 6 gameobjects that serve different roles in my game and i want to have the same script attached to them since they will act the same but have certain differences. I was thinking of having different classes for each role in the script. But since Unity will only access the class that bears the same name as the script i have reconsidered it.

How should i do it? I was looking up enums but i am unsure of how they work. Do you guys have any idea on how i should do? I wanna avoid having to make different scripts for each different gameobject, considering they will play like almost the same. The differences is in the way they move, interact with other objects and their position in the gameworld.

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

Answer by Gobaz · May 28, 2016 at 09:15 AM

If you dont want to use inheritance or abstraction (would require each role having its own class inheriting from a general parent.

Then i would probably use an enum.

 public enum RoleType
 {
     Knight,
     Ranger,
     Mage
 }
 
 public class Character : Monobehaviour
 {
     public RoleType; // Set this value in the inspector for each gameobject
     
     private void SomeAction()
     {
         switch(RoleType)
         {
             case Knight :
                 DoKnightActions();
                 break;
             case Ranger :
                 DoRangerActions();
                 break;
             case Mage :
                 DoMageActions();
                 break;
         }
     }
 }

Comment
Add comment · Show 5 · 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 cjdev · May 28, 2016 at 09:35 AM 0
Share

As an aside and to address the technical aspect of enums: they're really just a labeled list of integers. $$anonymous$$ind of like an array of integers with a unique name for each, they can be cast back and forth to an int and used for an index or other things like that. It's also the reason that the switch statement works with them, because they're really just integers behind the scenes.

avatar image JedBeryll · May 28, 2016 at 09:40 AM 0
Share

This is a good solution too but this involves a switch check in every update or fixedupdate (if it's not a single action) which is unnecessary. And in update or fixedupdate you want to avoid those if you can. A few won't cause a problem but it's not a good practice because it can affect performance if you do this a lot.

avatar image cjdev JedBeryll · May 28, 2016 at 09:48 AM 0
Share

I agree that inheritance is the better solution overall, but if the OP doesn't want to add any classes for whatever reason then I think this is the optimal choice. It also doesn't necessarily involve the switch check in every update, only when the method is called.

avatar image JedBeryll cjdev · May 28, 2016 at 09:55 AM 0
Share

There's a third option: Actions. I'll explain if anyone needs it. $$anonymous$$ethods can be saved as Action. It requires the System namespace. you create an action like this:

  Action act;
 
 void Start() {
     act = $$anonymous$$y$$anonymous$$ethod;
     //actually there's more to it but this is the easiest way.
 }
 
 void Update() {
     act.Invoke(); //calls the method
 }
 
 public void $$anonymous$$y$$anonymous$$ethod() {
     //whatever
 }

For the record i still think inheritence is the best because classes can hold their own variables and the $$anonymous$$onoBehaviour class doesn't need to hold them if they are unnecessary.

avatar image vittu1994 · May 28, 2016 at 02:24 PM 0
Share

Hey thanks! I was unsure on enums because i forgot how to access them through the inspector. I wont be needing to add any more roles so this will look good for me! :)

EDIT: Note for everyone else, you will need to also name the declared enum variable inside the class or else it will not run, so remember that! ^^

avatar image
1

Answer by JedBeryll · May 28, 2016 at 09:28 AM

One good way is to create certain behaviors with inherited classes. For example: You have a monobehaviour script called VehicleController and in that a class called AIBehaviour, and have sub classes called CarMovement and MotorcycleMovement. They have virtual and override functions that you can call in udate or fixedupdate or however you want. It would look like this:

alt text

More info on inheritance: https://msdn.microsoft.com/en-us/library/ms173149.aspx


inheritance.png (50.4 kB)
Comment
Add comment · Show 2 · 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 vittu1994 · May 28, 2016 at 02:28 PM 0
Share

Thanks, this looks useful on how to have several classes in one script, i didnt know it could be done in Unity :) ill bookmark it but i found a simpler solution with enums now and i think its more what i wanted. Thanks anyways!

avatar image JedBeryll vittu1994 · May 28, 2016 at 06:20 PM 0
Share

Sure thing!

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

46 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

Related Questions

Unknown identifier: 'types'. js(38,61) 1 Answer

Getting NullReferenceException when creating class instance using List 1 Answer

Need assistance with SubType in custom class (UnityScript or C#) 2 Answers

Instance of the class represented by Game Object 0 Answers

Why are my PlayerPrefs not showing correct values? 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