FlamingGenius
FlamingGenius

Reputation: 216

Get components that derive from base class c#

I am working in a c# environment and am creating a base class and would like to have other classes like StormyWeather.cs or SunnyWeather.cs and have these classes derive from the base class BaseWeather.cs

public class BaseWeather: MonoBehaviour{
    public ParticleSystem particleSystem;
    public AudioClip weatherAudio;

    public string Name;
    public float AudioFadeTime = 0.25f;

    public float LightDimTime = 0.1f;
    public float MinimumIntensity = 0f;
    public float MaximumIntensity = 1f;
    public float Intensity = 0.25f;

    private void Start(){
        particleSystem.enableEmission = false;
    }

    public virtual void Weather (){
        if (WeatherSystem.Instance._debugState == DebugLevel.Full) {
            Debug.Log (Name + " Starting at: " + Time.timeSinceLevelLoad);
        }

        particleSystem.enableEmission = true;
    }
}

So in turn the SunnyWeather.cs might look like this

public class SunnyWeather : BaseWeather {
    public override void Weather(){
        base.Weather ();
    }
}

How do I look/get the first script on a gameobject that derives from the BaseWeather.cs

To clarify I am using unity and the SunnyWeather.cs will be attached to a gameObject I would like to know how I find the First Component on a gameObject that is inherited(for a better term) from the BaseWeather.cs class

The reason for this is I will have multiple scripts for each weather like SunyWeather.cs, StormWeather.cs, SnowWeather.cs and they all derive from the BaseWeather.cs class

EDIT:

I am currently using this to call it but do not know if it is the right way to do so GetComponent<BaseWeather>().Weather();

Upvotes: 0

Views: 2565

Answers (2)

mrogal.ski
mrogal.ski

Reputation: 5940

To answer most of your concerns I could say that yes, you're currently doing it the right way but only if it fits your needs.

GetComponent will return the first component that matches your query which in that case is type assignable by the component type. Meaning that if you put the base class in your query, it will return you the first occurrence of the type that derives or is that type.

To ensure that's the right component you can place it on top of other components using inspector view or filter it out by calling GetComponents and then iterate through all of the matching components.


Shortening my answer, if you have more than one BaseWeather component you should use GetComponents<BaseWeather> method and filter out the one you want in any other case just use GetComponent<BaseWeather> method.

Upvotes: 3

KelvynRisso
KelvynRisso

Reputation: 82

If I understood the question, you can identify the inheritance using:

    (from assemblies in AppDomain.CurrentDomain.GetAssemblies()
        from types in assemblies.GetTypes()
        where typeof(IWeatherState).IsAssignableFrom(types)
        select types
    ).ToArray();

But, to you get the First, you will need to do something hardcoding, like a Queue or Stack.

Upvotes: -2

Related Questions