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
0
Question by BrimTheOne · May 11, 2014 at 01:39 PM · c#classesparent-childtype

[C#]how to use child class type in parent class method.

Hello, my problem is this here -

onUse Method in ITEMCREATOR.class

     public int onUse(ITEMCREATOR modifier){
         if(modifier.GetType() == typeof(POTION)){
             if(modifier._TYPEID == 1){
                 return (GainHealth(modifier));
             }else{
                 return 0;
             }
         }else{
             Debug.Log("this item has no external function");
             return 0;
         }
         }

GainHealth method in ITEMCREATOR.class

     public int GainHealth(POTION modifier){
         int changed = 0;
         PlayerStats.HEALTH += PlayerStats.MAX_HEALTH / modifier._MODIFIER;
         //Debug.Log(changed);
         return PlayerStats.HEALTH;
     }

I have a ITEMCREATOR.class, where this onUse() is located.

and i have a POTION class that is derived from my ITEMCREATOR.class.

This of course gives me a error, because _MODIFIER is a part of my POTION class, not my ITEMCREATOR class.

I know the problem, i just dont know how to fix it, and if there even is a way to make it work.

Any help is greatly appreciated.

NOTE: Also, if my grammar seems a bit messy or flauded, it's because English is not my primary language. Hope you will understand :)

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 Deign · May 11, 2014 at 03:15 PM

The best way to handle this is to make the onUse function virtual and override in the potion class. Here are some references for you to read through that will explain it a lot better than I would be able to:

Virtual

Override

Comment
Add comment · Show 7 · 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 BrimTheOne · May 11, 2014 at 03:21 PM 0
Share

I have read a bit about virtual, and i do know a little portion of what "override" does.

But i dont seem to be able to understand the concept , and how to implement it into my own code.

Would i have to create the method in both classes(with exactly the same content, and virtual) and override the one in POTION class?

avatar image Deign · May 11, 2014 at 03:44 PM 0
Share

I'm not entirely sure what you're trying to accomplish, but I think the effect you're looking for can be accomplished with the following code.

     public class ITE$$anonymous$$CREATOR
     {
         public int _TYPEID = 0;
         public int _$$anonymous$$ODIFIER = 0;
         public virtual int onUse() {
             Debug.Log("this item has no external function");
         }
 
         public virtual int GainHealth() {
             Debug.Log("this item has no external function");
         }
     }

     public class POTION : ITE$$anonymous$$CREATOR
     {
         public override int onUse() {
             if(_TYPEID == 1)
             {
                 return (GainHealth());
             }
             else
             {
                 return 0;
             }
         }
 
         public override int GainHealth() {
             PlayerStats.HEALTH += PlayerStats.$$anonymous$$AX_HEALTH / _$$anonymous$$ODIFIER;
             return PlayerStats.HEALTH;
         }
     }
avatar image BrimTheOne · May 11, 2014 at 03:59 PM 0
Share

You see, _$$anonymous$$ODIFIER and _TYPEID is part of POTION class, and i would like it to stay that way, if possible :) Else, the script you made would be perfectly fit.

avatar image Deign · May 11, 2014 at 04:58 PM 0
Share

Not a problem. Neither of those fields are referenced in ITE$$anonymous$$CREATOR, so just add them to Potion :)

avatar image BrimTheOne · May 11, 2014 at 05:08 PM 0
Share

But, the $$anonymous$$ethod needs a new instance of POTION, to know what tier it should work with. But, since ITE$$anonymous$$CREATOR does not contain _$$anonymous$$ODIFIER, it wont let me do this

     public override int onUse(BASEITE$$anonymous$$ modifier) {
         if(_TYPEID == 1)
         {
             return (GainHealth(modifier));
         }
         else
         {
             return 0;
         }
     }

Also, i feel like a dumb donkey right now, for not clarifying good enough earlier. I dont like to waste peoples time. :S

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Script on two objects only runs as a single instance? 2 Answers

How do I run Start() inside a nested class? 1 Answer

Cant access the Image icon from class 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