- Home /
[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 :)
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:
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?
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;
}
}
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.
Not a problem. Neither of those fields are referenced in ITE$$anonymous$$CREATOR, so just add them to Potion :)
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613144757im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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