- Home /
Too subjective and argumentative
How to increase a variable for individual prefab when using inheritance
I recently learned about inheritence and polymorphsm. If I'm able to individually modify their stats with this method how would i upgrade their stats if say i had 2 prefabs? For example.
/
Lets say i have a super class to contain my variables called SuperSpell
/
public abstract class SuperSpell : MonoBehaviour
{
public string NAME;
public int LEVEL;
public int COST;
public int OUTPUT_GOLD;
public int OUTPUT_SPELL;
public float ATK;
public float DURATION; //60 Seconds
}
/
Now I have a child class called BTN_Spell_Controller. This script is inheriting SuperSpell and 2 prefabs are using this script. I'm able to individually modify their stats in the inspector.
/
/
For BTN_Spell_controller I added static variables to store the inherited variable for the prefabs.
/
public class BTN_Spell_Controller : SuperSpell
{
public static int level;
public static int gold_output;
public static int spell_output;
public static float duration;
public static float attack;
private void Start()
{
level = LEVEL;
gold_output = OUTPUT_GOLD;
spell_output = OUTPUT_SPELL;
duration = DURATION;
attack = ATK;
}
// Use this for initialization
void Start()
{
}
/
This part is where i get confused. Finally I have a third script called BTN_UI_UPGRADE used to upgrade my one of my prefab's inherited variables through a button press. Note this class is inheriting BTN_UI_UPGRADE and grabbing the static classes (Note sure if thats a bad idea)
/
public class BTN_UI_UPGRADE : BTN_Spell_Controller
{
public void Add()
{
gold_output += 1;
}
}
/
The problem is I want the upgrade to affect only one of my prefabs and I know this affects all of them. I'm unsure what strategy to use in this case. Sorry if my explanation was too long or not enough info. Thanks!
I can't make head or tail of your use of static variables here. It's clearly wrong, but I've no idea what it's trying to achieve. Why don't you just leave all that out. Your subclass inherits all the fields in its base class, you don't need to copy them into the subclass... just use them.
Also, you say "grabbing the static classes" but there are no static classes here.
I think you need to read up on the use of the static
keyword. There are plenty of resources if you google "static keyword c#" but also plenty of answers on here that already explain it.
Answer by HappiiGamer · Oct 16, 2019 at 02:22 PM
I just noticed that your gold_ouput is static!? I wrote my first answer thinking it not to be static.
Make your fields non static, and look up what is static.
This is wrong. gold_output
is static, so if you change its value then yes, you do change its value for all instances.
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
How to change variables from another script? 1 Answer
How can I access an inherited method from a separate (collided) object? 1 Answer
Calling methods on inherited classes in C# 2 Answers
C# Inheritance - Reference the INSTANCE of a variable, is it possible? 1 Answer