- Home /
Accessing the Variable of an Inherited Scriptable Object
I have a Scriptable Object class called Ability, which inherients Scriptable Objects:
public class Ability : ScriptableObject {
public new string name;
public string aDesc;
public int aCoolDown;
public Sprite aSprite;
}
And a child of Ability called BasicOffensiveAbility, which inherits Ability:
public class BasicOffenseAbility : Ability {
public Sprite pSprite;
public float pSpeed;
public bool allEnemiesAffected = false;
}
Since there could be multiple children of Ability, I check the type, and I want to access a variable within BasicOffenseAbility, i.e. pSpeed:
public void UseAbility(Ability a){
if(a is BasicOffenseAbility) {
LaunchProjectile(a.pSpeed);
}
}
But I cannot seem to access the variable of pSpeed, but only the base variables of Ability ( name, aDesc, aCoolDown, etc.).
So, is there some way to access the variables of an inherited Scriptable Object?
Where is the "UseAbility" method located? Since I cannot see the class, I would make a guess to assume that the method is in a class does not inherit from "BasicOffenseAbility" although I cant say for certain. Perhaps add the class definition where "UseAbility" resides?
Answer by Mako-Infused · Apr 20, 2018 at 02:04 AM
You can ignore my comment, since I looked at it more careful and see now that you're passing in the base class "Ability". Although you can use the "is" keyword to determine whether a class inherits from a particular type you still would have to upcast it to the correct type in order to access its variables.
An example of that might look something like the following:
public void UseAbility(Ability a)
{
if(a is BasicOffenseAbility)
{
var boa = a as BasicOffenseAbility;
LaunchProjectile(boa.pSpeed);
}
}
Good luck!
Thank you, this is exactly the simple solution I knew existed!