Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 csharp1 · Sep 19, 2020 at 02:04 PM · charactermultiple objectsstatssame

Best way to create multiple characters with the same variables for stats, but different amounts and abilities.

In my game I intend on having multiple characters that will all have health, defence, attack etc. but each character will have different values for their health, defence, attack and so on. This is mostly fine, as this can mainly be sorted out on the prefab as I can change public variables and save it to that specific character.


In addition to the basic stats, each character will have fundamentally different abilities, with different effects, animations e.g. some characters will have ranged abilities, some will be melee attacks, some will just affect the beforementioned stats.


I want to know the most effective way to have the stats and abilities change depending on what character is selected.


I have a couple ideas on how to tackle having these multiple characters, but I'm not sure how resource intensive they may be, or just how unnecessarily complicated I'm making it.


  • Idea 1: My initial thought was just having a separate script for each character with only their specific stats and abilities, but this seems inefficient as I would have duplicate code for every character (but would get around the issue in idea 2)


  • Idea 2: My next thought was to have just one massive script that would be used for every character. The idea here is that when the game starts, the character would be identified by a public variable and their abilities assigned by this same variable. Then, when an ability is used, the game checks what character is being played, and then play the ability corresponding to the same public variable from earlier. I'm not sure if this is inefficient or not as I'm not too sure how intensive it would be to check the character every time the ability is used or if there is a way around this.


I'm still very new to C# and programming in general so there's likely a better way to the above. Any help is greatly appreciated.


Below is some code I've created for my second idea and it seems to work, but as I said it may not be the best way to do this as it uses the update function a lot, and will use it even more when there are multiple abilities per character.


 public int maxHP;
 public int AttackDMG;
 public int defence;
 
     public string charClass;
 
     private void Update()
     {
         if(Input.GetKeyDown(KeyCode.E))
         {
             ChangeCharacterAbilities(charClass); //check what class is being used
         }
     }
     public void ChangeCharacterAbilities(string className)
     {
         switch (className)
         {
             case "Knight":
                 KnightUtility(); //play ability is class == knight
                 break;
             case "Ranger":
                 RangerUtility();
                 break;
         }
     }
     void KnightUtility()
     {
         print("Use Knight utility ability"); //ability for knight class
     }
 
     void RangerUtility()
     {
         print("Use Ranger utility ability");
     }
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
1
Best Answer

Answer by TheonlysiQ · Sep 19, 2020 at 11:14 PM

Hmm, you can do this in a single script. However this script would include some other C# classes (not unity monobehavior scripts, but simple classes). Basically what you need is Inheritance and Interfaces (read about them on c# docs - they are quite useful concepts available in a lot of programming languages)


You can design your classes somewhat like this (definitely not the only way):

GeneralDude class - contains methods to walk, run, eat, sleep, whatever common action every type of character can do - and which is the same for all types of characters.

<->

ParticularActionsInterface class - contains "contract" methods (which are basically methods that are only declared - no body) that generally map the actions that you want to make different for each character type. Let's say that each character should have 2 actions which are unique. You can have in that interface 2 methods: uniqueaction1() and uniqueaction2(). <->

Next, you should create 1 class per each character type you wish to have. Every one of these classes will inherit the GeneralDude class and also the ParticularActionsInterface class. This will have the following effects:

1) classes which inherit the GeneralDude class will now have the public methods from GeneralDude available and ready to be used.

2) classes which inherit the ParticularActionsInterface class will force you to provide definitions for the methods in ParticularActionsInterface. Here you should provide unique behaviors for your actions, in the methods uniqueaction1() and uniqueaction2().


After that, you include the class files to your Unity script and create your global variables (one for each character type). When you assign one of the character types, the particular actions will change based on the interface methods definitions you wrote for each character type class.

Now your life is made easier because you will mostly call the same methods no matter what the current character type is (ie. both warriors and hunters can walk, run, or perform uniqueaction1 or uniqueaction2). Also neat is that for the most part, you won't have duplicate code.

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 csharp1 · Sep 20, 2020 at 07:11 AM 0
Share

Thanks so much!

I do remember learning about inheritance once before but completely forgot what it was called or how to implement it which made searching for it very troublesome. I've looked into Inheritance and Interfaces and used this video for interfaces and this video for inheritance for those that may come across this question in the future. They've given me a decent understanding on how to impliment both into my project.

avatar image TheonlysiQ csharp1 · Sep 20, 2020 at 10:18 AM 1
Share

Nice resources, they look helpful. Good luck on your project!

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

188 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

Related Questions

Need help with Player stats for Choose your own story game 1 Answer

How to make classes? 2 Answers

I am trying to make a shop system but I am having this doubt 1 Answer

Help with animation... Please 2 Answers

Character Flying around uncontrollably 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