0

In Unity I have a gameobject with a trigger attached. This trigger listens to the enter, exit and stay event.

When events get executed the colliding object gets checked for a specific interface component. If this interface component is not null / is attached the code should call a method from the interface component.

Currently I do this

public class LightSource : MonoBehaviour
{
    private void OnTriggerEnter(Collider col)
    {
        HandleLight(col, LightAffectableAction.Enter);
    }

    private void OnTriggerExit(Collider col)
    {
        HandleLight(col, LightAffectableAction.Exit);
    }

    private void OnTriggerStay(Collider col)
    {
        HandleLight(col, LightAffectableAction.Stay);
    }

    private void HandleLight(Collider col, LightAffectableAction action)
    {
        ILightAffectable lightAffectable = col.GetComponent<ILightAffectable>();

        if (lightAffectable != null) // Is the component attached?
        {
            switch (action)
            {
                case LightAffectableAction.Enter:
                    lightAffectable.EnterLight();
                    break;

                case LightAffectableAction.Exit:
                    lightAffectable.ExitLight();
                    break;

                case LightAffectableAction.Stay:
                    lightAffectable.StayInLight();
                    break;
            }
        }
    }

    private enum LightAffectableAction
    {
        Enter,
        Exit,
        Stay
    }
}

but I really don't like using a switch and enum. Maybe a bunch of gameobjects within the trigger will cause performance problems.

Some methods contain a out parameter and I thought about creating something like this

public class LightSource : MonoBehaviour
{
    private void OnTriggerEnter(Collider col)
    {
        if(col.TryGetComponent(ILightAffectable, out ILightAffectable comp)) // Pass in the component type
        {
          comp.EnterLight();
        }
    }

    private void OnTriggerExit(Collider col)
    {
        if(col.TryGetComponent(ILightAffectable, out ILightAffectable comp))
        {
          comp.ExitLight();
        }
    }

    private void OnTriggerStay(Collider col)
    {
        if(col.TryGetComponent(ILightAffectable, out ILightAffectable comp))
        {
          comp.StayInLight();
        }
    }
}

but I don't know how to create an extension method like TryGetComponent that fits to the example code above.

I pass in a component type as parameter and get the component as a out parameter.

How can I create such a method?

1 Answer 1

1

You could create a generic extension method just like the existing GetComponent method.

public static class ColliderExtensions
{
  public static bool TryGetComponent<T>(this Collider collider, out T component) where T : class
  {
    component = collider.GetComponent<T>();
    return component != null;
  }
}

Usage:

ILightAffectable component;
if (col.TryGetComponent(out component))
{
  component.ExitLight();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.