#Gizmos for Transform of UnityEvents Listener

1 messages · Page 1 of 1 (latest)

spiral night
#

I want to draw line between all listener with gizmos. However, there is a problem that gameObject is null.
I found that it only success for the listeners with "GameObject" method, but not other custom scripts. Is there any way to fix it? Thanks.

Here's my code

private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        for (int i = 0; i < UnityEvents.GetPersistentEventCount(); i++)
        {
            GameObject go = UnityEvents.GetPersistentTarget(i) as GameObject;
            Gizmos.DrawLine(this.transform.position, go.transform.position);
        }
    }
weak pond
#

Should be relatively simple, you just have to account for multiple types: cs var position = UnityEvents.GetPersistentTarget(i) switch { GameObject go => go.transform.position, Transform tr => tr.position, Component comp => comp.transform.position, _ => transform.position // object's own position as fallback };

spiral night
#

Thanks! It works now.
Btw, any keywords can study this kind of coding/structure more? Is my first time seeing this (the switch thing). Thanks again!

weak pond
#

Look up switch statements.
They used to be written like this: cs switch(value) { case Constant1: // code break; case Constant2: // code break; ... }
The syntax I used above is newer and more compact, but functionally the same.

spiral night
#

Thanks a lot