Accessing the Variable of an Inherited Scriptable Object
Asked Answered
A

1

0

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?

Alesha answered 20/4, 2018 at 0:17 Comment(1)

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?

Gyniatrics
G
0

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!

Gyniatrics answered 25/6, 2024 at 3:35 Comment(2)

Thank you, this is exactly the simple solution I knew existed!

Alesha

Thank you !!

Motion

© 2022 - 2025 — McMap. All rights reserved.