- Home /
How to see a variable from a lower inheritance
I have a class called Weapon and a class called Poison which inherits from Weapon. In a different script, I have a Weapon[] with different weapons stored inside. One of the items in the Weapon[] is of type Poison, and in other parts of the script I want to see the variable from the Poison script, but it has a base type of Weapon, so it wont let me see them, how do I do this?
My Code:
private void ChangeWeapon()
{
currentWeapon = weapons[selectedID];
player.currWeaponDamage = currentWeapon.damage;
if (currentWeapon.name == "NULL")
{
player.canCanKill = false;
weaponSelectedText.text = "Current Weapon: None";
}
else
{
player.killCooldownTime = currentWeapon.attackSpeed;
player.canCanKill = true;
weaponSelectedText.text = "Current Weapon: " + currentWeapon.name;
}
if (currentWeapon.abilityID == 1)
{
player.isCurrWeaponPoison = true;
player.poisonTime = currentWeapon.poisonTime;
player.poisonDamage = currentWeapon.poisonTime;
}
else
{
player.isCurrWeaponPoison = false;
player.poisonTime = 0;
player.poisonDamage = 0;
}
}
private void InitializeWeapons(int numOfWeapons)
{
weapons = new Weapon[numOfWeapons];
weapons[0] = new Weapon(0, 0, "NULL", 0, 0);
weapons[1] = new Weapon(1, 10, "Bat", 10, 0);
weapons[2] = new Weapon(2, 15, "Knife", 15, 0);
weapons[3] = new Weapon(3, 30, "Sword", 30, 0);
weapons[4] = new Weapon(4, 15, "Poison Staff", 5, 1, 5, 5);
}
and the class scripts:
public class Weapon
{
public int ID;
public int attackSpeed;
public string name;
public int damage;
public int abilityID;
public Weapon(int getID, int attkSpeed, string getName, int getDamage, int abilityId)
{
ID = getID;
attackSpeed = attkSpeed;
name = getName;
damage = getDamage;
abilityID = abilityId;
}
}
And:
public class Poison : Weapon
{
public int poisonTime;
public int poisonDamage;
public Poison(int getID, int attkSpeed, string getName, int getDamage, int abilityId, int getPoisonTime, int getPoisonDamage) : base(getID, attkSpeed, getName, getDamage, abilityId)
{
ID = getID;
attackSpeed = attkSpeed;
name = getName;
damage = getDamage;
poisonTime = getPoisonTime;
poisonDamage = getPoisonDamage;
}
}
I'm guessing you would have to create a fresh new instance of Poison, with the same fields of poisonTime and poisonDamage in the Weapon script to get the values at runtime from poison.
I'm also guessing you tried both-way inheritance (if that even works) so both classes inherit each other? @TheThunderIron
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Is it possible to change value on base class when the same field gets changed on it's derived class 0 Answers
How to prevent an inherited variable from showing up on a scriptable object? 1 Answer
Strange inheritance behaviour (Two different values) 1 Answer
How to work with a list of base class, if a variable is only found in some subclasses? 2 Answers