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 Laminant · Oct 24, 2016 at 06:36 AM · classescomponentsbest practices

Setting values in components best practice

Nubby question. So let's say I have an HP component, a Shield component, and a Damage component for a unique character "Bob".

What would be the best way to configure that character's specific values for each of the components?

A single configuration component like "BobConfig" that has access to and changes the values of each of them? That seems like that defeats the purpose of the modularity of component based architecture when there is a component that accesses all the other components.

For example with the HP and Shield components:

 using UnityEngine;
 using System.Collections;
 
 public class HP : MonoBehaviour {
 
     public float hp = 100f;
 
     void Start () {
     
     }
     
     void Update () {
         //Functions for HP
     }
 }
 
 public class Shield : MonoBehaviour {
 
     public float shield = 100f;
 
     void Start () {
     
     }
     
     void Update () {
         //Functions for Shield
     }
 }

Then we have the BobConfig that alters the values in those components:

 using UnityEngine;
 using System.Collections;
 
 public class BobConfig : MonoBehaviour {
 
     private HP _HP;
     private Shield _Shield
 
     void Awake () {
         _HP = GetComponent<HP>();
         _Shield = GetComponent<Shield>();
     }
     
     void Start () {
         _HP.hp = 150f;
         _Shield.shield = 200f;
     }
 }
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 meb111 · Oct 24, 2016 at 07:29 AM

I think the general way most people deal with this type of thing would have a class or if you want SLIGHTLY more advanced you could use inheritance. Basic example would be

 using UnityEngine;
 using System.Collections;
 
 public class Character
 {
      //here we have our stats and other goodies could add more
      public int health;
      public int shield;
      public int damage;

      //this is our constructor so when we what to make a Character we can set the stats at that point
      public Character(int health, int shield, int damage)
      {
            health = health;
            shield = shield;
            damage = damage;
      }
 
      //this is a simple function that will more explicitly modify our health/shield stat based on 
      //a hit we may take. if we want since health and shield are both public variables we can 
      //modify them directly
      public void Damage(int damageToCharacter)
      {
            if(shield>0)
            {shield-=damageToCharacter;}
            else
            {health-=damageToCharacter;}
            //you can get much smarter here where if you have 5 shield and 10
            //health and take a 10 hit shot you would make shield 0 and hp 5 etc.
      }
 }

then you would have your 'Bob' prefab contain a script that instantiates this class as itself. For example using UnityEngine; using System.Collections.Generic;

 public class CharacterController : MonoBehavior
 {
      Character Bob;
      void Start()
      {
            //this is where we create a character we call it Bob and set it's 
            //starting values. and so long as the gameObject exhist in the game
            //this exact version of Bob will exist and you can change values
            //call Character specific functions on Bob etc.
            Bob = new Character(50, 100, 15);
      }
      void Update()
      {
           //add control and hit checking here
           //if we were hit we would call Bob.Damage(15) (15 or whatever damage value you want)
           //that would automatically do the function in our Character class called Damage
           //we can also change values directly such as if we pick up a health pack 
           //Bob.health += 50;
      }
 }

To further that you can look into Inheritence.

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 Laminant · Oct 24, 2016 at 07:54 AM 0
Share

Thanks for the reply!

I'm looking for more of a component based answer ins$$anonymous$$d of inheritance based.

So "HP", "Shield", and "Damage" would be their own classes that handle their individual responsibilities.

Or do you think inheritance would be the best option in this situation?

From what I've been reading, it seems like everyone is saying components are typically the best answer for most situations.

I'll edit the topic to be more clear.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Disable a group of scripts based on base class. 0 Answers

Is it better to split a script into different scripts? 1 Answer

Can't load array from another class 0 Answers

Access child class variable inside child class script as a parent class of the parent script 0 Answers

Calling Function from another script not working 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