I currently have my equipment set up as the below, however I wanted to split up my Equipment script into Weapons/Armour with their respective variables.
The problem im having, is I am unable to refer to a variable in the currentEquipment array.
E.g currentEquipment[7].AttackSpeed; (Dont worry about the element of the array, thats been sorted)
I understand why, because its referring to the parent and not the child, which could have many children. Is there a way in the currentEquipment to refer to the child variables of the instanced Class?
public class BaseEquipment : MonoBehaviour {
public Equipment[] currentEquipment;
}
Do you mean that the Weapon and Armor classes inherit from (are subclasses of) the Equipment class, and you have a list of Equipment, some of which are actually Weapons and Armors? In that case, you need to downcast from the equipment class to the subclass you know it is.
Equipment[] myEquipment; // gets populated somehow
Equipment weaponEq = myEquipment[2]; // or whatever it is
Weapon theWeaponItself = weaponEq as Weapon; // This is a safe cast. If weaponEq isn't a weapon for some reason, theWeaponItself will be null after this
if(theWeaponItself != null)
{
// The cast was successful
float attackSpeed = theWeaponItself.AttackSpeed; // This will now work and you can do whatever you want with it
} else
{
// The cast failed and it is a sad day
Debug.LogError("Could not cast to Weapon: " + weaponEq.ToString());
}