Custom property in a class derived from Ability

FadelMS

Member
Hi,
Is there a way to add a custom property to a class derived from Ability?
And then access the property from the return of GetAbility()?
For example:

Code:
public class MyAbility : Ability
{
  [HideInInspector][SerializeField] public float myProperty;
  public float My_Property { get => myProperty; set => myProperty= value; }
}
And then in a MonoBehaviour class, I access the custom property as follows:
Code:
Ability myAbility = m_characterLocomotion.GetAbility<MyAbility >();
myAbility.My_Propery = 1;
 
Yes, what you are doing makes sense. The code that you posted should work. GetAbility will cast to MyAbility and then you can access the property.
 
Thanks Justin,
It seems I'm missing something. The code mentioned above does not work. It works only if added to the base Ability class.
I guess GetAbility() returns an instance of the base Ability class not the derived class. Casting to MyAbility didn't work either.
 
Last edited:
GetAbility<T> will return an instance of that class, not the base ability class. I just tried the following and it worked:

Code:
            var jump = m_CharacterLocomotion.GetAbility<Jump>();
            jump.Force = 4;
 
Now it works.
The problem was in this line of code: (My mistake)
Code:
Ability myAbility = m_characterLocomotion.GetAbility<MyAbility >();
I changed the code to:
Code:
MyAbility myAbility = m_characterLocomotion.GetAbility<MyAbility >();

Thanks Justin for your help.
 
Top