Skip to content

Listener Tracking

Attach a button, a slider, or a C# event, then keep editing. When you Stop, or press Run again, ZenCoder lets go of every listener that run added. The next Run is the only script still hooked to that button.

This is on by default.

ZenCoder → Preferences → General → Track Listeners

1.2.0 · Shoshin

Introduced in the v1.2.0 release.

A button

public UnityEngine.UI.Button button;

public void OnEnable()
{
    Listen(button.onClick, OnPressed);
}

private void OnPressed()
{
    transform.localScale *= 1.1f;
}

Press Run and click the button. The object grows. Press Stop. Further clicks do nothing, because that run has let go. Press Run again and only the new script is attached.

Call Listen from OnEnable or Start. Each new run subscribes again.

A value

public UnityEngine.UI.Slider slider;
float speed = 1f;

public void OnEnable()
{
    Listen(slider.onValueChanged, value => speed = value);
}

Drag the slider. speed follows it. Stop the script, and the slider no longer writes into the old run.

A C# event

public Door door;

public void OnEnable()
{
    Listen<System.Action>(
        h => door.Opened += h,
        h => door.Opened -= h,
        OnOpened);
}

private void OnOpened()
{
    Debug.Log("The door opened");
}

The handler in the middle is the one that gets removed. Pass the event's add and remove, then the method to call. That works for any C# event, not only Action.

Leave them attached

Turn Track Listeners off.

Listen still subscribes. Stop and Run no longer remove those listeners, so they stay until you remove them yourself or leave Play Mode.


← Hot State Safe Execution →