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 /
avatar image
1
Question by Sendatsu_Yoshimitsu · Jan 22, 2015 at 01:58 AM · c#inheritanceclasses

Problem using variable of type CustomStat to indicate which player statistic to change

I'm trying to create a single, highly modular Buff class that anything which wants to alter player stats can call to alter any stat. I have a list of type UtilityStat called statList, UtilityStat being a custom stat class that contains a bunch of floats: health, MP, and so forth. If I want a method to create a debuff that slowly reduces HP, I want to go:

 UtilityStat targetStat = UtilityStat.HP; //This is what we're reducing
 Buff myBuff = new Buff(); //Create a new, empty buff that does nothing
 myBuff.AddToStatList(targetStat, -1f); //Add a new effect to myBuff, "Add -1 to targetStat every tick"


To do this, I added the following in my root Buff class:

 public List<UtilityStat> statList;        //List of stats to be altered
             public List<float> statChange;            //Amount to alter each stat by
 
             public void AddToStatList(UtilityStat stat, float change){//External functions run this to add values to statList & statChange
                 statList.Add (stat);
                 statChange.Add (change);
             }
 
             public override void UpdateEffect (Character character){
                 for (int i = 0; i < statList.Count; i++) {
                     character.utilityStat.statList[i] += Mathf.Clamp(statChange[i],0f,100f); //Iterate through statList, and for each index, find the matching utility stat on the player and increment it by statChange[i]
                 }
             }

However this returns an error on the line that starts character.utilityStat.statlist[i], namely "Type 'UtilityStat' does not contain a definition for 'statList' and no extension method 'statList' of type 'UtilityStat' could be found". Is what I'm trying to do possible, or would I need to create multiple buff classes that each specifically target a single value in the player's UtilityStat list?

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

Answer by InvincibleCat · Jan 22, 2015 at 02:08 AM

because the error on

 character.utilityStat.statList[i]

is that statList is not a member of your class UtilityStat

And

 statList[i]

Exists but is not a float

Maybe you should have your UtiltyStat class like:

 public class UtilityStat
 {
     public float Value = 0.0f;
 }

And then:

  statList[i].Value += statChange[i];

Also,

 Mathf.Clamp(statChange[i],0f,100f);

Doesn't allow negative value. So

 myBuff.AddToStatList(targetStat, -1f); 

won't do anything

Comment
Add comment · Show 16 · 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 Sendatsu_Yoshimitsu · Jan 22, 2015 at 02:19 AM 0
Share

Yea, character.UtilityStat is a single UtilityStat, which is a class that contains six floats (HP, $$anonymous$$P, etc), so I was trying to target a specific value in character.UtilityStat, let's say HP, by saying statList[0] = UtilityStat.HP.

And if Clamp doesn't take negative values, would I just want to run the calculation on two lines?

 Character.UtilityStat.Whatever += statChange[i];
 
 Character.UtilityStat.Whatever = $$anonymous$$athf.Clamp(
 Character.UtilityStat.Whatever, 0f, 100f);
avatar image InvincibleCat · Jan 22, 2015 at 03:36 AM 0
Share

And if Clamp doesn't take negative values, would I just want to run the calculation on two lines?

Yes this is better.

Can you share more of your code ?

avatar image Sendatsu_Yoshimitsu · Jan 22, 2015 at 03:42 AM 0
Share

Sure, what do you want to see? The utilitystat class itself just reads

 [Serializable]
 public class UtilityStat{
     public float HP;
     public float $$anonymous$$P;
 }


and the character class where the list I'm trying to access is just a data holder:

 public class Character{
             string name;
             UtilityStat utilityStat;
             Animator animator;
             List<StatusEffects>() effectsList;
         }


avatar image InvincibleCat · Jan 22, 2015 at 04:09 AM 0
Share

Ok so that makes sense. You need a public accessor to the utilityStat variable !

avatar image Sendatsu_Yoshimitsu · Jan 22, 2015 at 04:13 AM 0
Share

Public accessor as in setting utilityStat to public? That results in the same error...

Show more comments

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

26 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

Related Questions

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

FPS weapons class structure 1 Answer

Calling a method of a class that is a part of another class? 1 Answer

HideInInspector with inherited variables 1 Answer

Cram variables inside class, use inheritance, or something else? 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