- Home /
Can I access variables of scripts that inherit from abstract classes?
Hi there I have been trying to wrap my around the use of abstract classes and I can work out how to call functions of scripts derived from abstract classes but not how to access variables using the same method.
Here's my scripts:
public class InputManager: MonoBehaviour{
public GameObject exampleObject;
private AttackAction exampleScript;
void Update(){
if(Input.GetKeyDown("f")){
exampleScript = exampleObject.GetComponent<AttackAction>();
exampleScript.PrimaryAttack();
}
if(Input.GetKeyDown("d")){
exampleScript = exampleObject.GetComponent<AttackAction>();
Debug.Log(exampleScript.damage);
}
}
}
public class AttackAction: MonoBehaviour{
public int damage;
public virtual void PrimaryAttack(){}
}
public class SwordAttack: AttackAction{
public int damage = 10;
public override void PrimaryAttack(){Debug.Log("sword swipe");}
}
public class BowAttack: AttackAction{
public int damage = 8;
public override void PrimaryAttack(){Debug.Log("shoot bow");}
}
Now my problem is on Line 12 i.e. - (Debug.Log(exampleScript.damage);
My question is - "What do I have to do to access these damage variables?" (C# ideally)
Thankyou
p.s. Line8 does work. These different classes have been written on different scripts, and the gameObject which is assigned in the inspector to Input$$anonymous$$anager has either the SwordAttack or BowAttack script attached to it (but not both).
One thing you are doing wrong is that you have a damage variable in AttackAction and then a new damage variable in SwordAttack and BowAttack which both inherits from AttackAction.
Since damage is public in the base class the inheriting classes will have it.
You would only need to set the value of damage in the Start.
If your top class is just there to provide empty method and empty variables, you may use an interface ins$$anonymous$$d:
interface IAttackAction{
int damage{get;set;}
void Primary Attack();
}
Answer by Jamora · Jun 27, 2013 at 03:26 PM
Technically you do not have abstract classes, because an abstract class is defined with the keyword abstract. One way to solve your problem is to use an abstract getter, like the following:
public abstract class AttackAction : MonoBehaviour {
public abstract int GetDamage();
public abstract void PrimaryAttack();
}
public class SwordAttack: AttackAction{
public int damage = 10;
public override int GetDamage(){ return damage; }
public override void PrimaryAttack(){Debug.Log("Sword swipe: "+ damage.ToString()+" damage.");}
}
Now we have an abstract class, which expects all of it's children to provide an implementation for getting the damage and doing a primary attack. Because you treat exampleScripts
as AttackActions
, you will only have access to whatever is defined in AttackAction
. Thus, in line 12 of you snippet, you would call exampleScript.GetDamage().ToString()
Also worth noting is that if AttackAction
will never contain any implementation (=code inside methods), it should be declared as an interface. Then people who want to see implementation know not to look there.
Answer by moonstruck · Jun 27, 2013 at 02:49 PM
You can access variables from the base class the same way as you access other variables. However there might be a problem because you redefine damage in the derived classes and they hide the variable from the base class. So you may use Awake to initialize the damage in each class, but define it only in AttackAction.
I apologize if I have not understood you correctly but the problem is that I don't want to access the damage from the base-class, I want to access it from the derived classes. So that when I press "d" it prints the damage of whichever derived class/script is attached to the gameObject in question. I want to be able to attach multiple of these scripts to different objects and make each of the damage variables a different value depending on which gameObject they are attached to, so if I initialize the base-classes damage value, it wont help when it comes to accessing the derived damage values.
Yeah, if you remove damage definitions from the derived classes than you will get exactly what you want. Think of the derived class as the base class plus something. So when you initialize the damage either from Awake or from the inspector damage will appear like it is contained in any of attack classes.
Ah ok, thankyou. I understand a bit more now. Cheers!
Your answer
Follow this Question
Related Questions
How to access variable from other class? 1 Answer
[C#] Is there a way to call static variables directly, independent from parent class? 3 Answers
Access to a variable inside a C# class 2 Answers
How to Typecast JS Variables as C# Classes? 0 Answers
how to acess Static variable in other scripts without extended functions? 2 Answers