With a toggle we can use a unityEvent to call function with dynamic parameters, like if toggle is ON then otherThing.gameObject.setActive() is also ON.
Very usefull to save script use, but can it be inversed?
I would like : if toggle is ON, then other gameObject is OFF.
1 Like
I think this is an elegant solution:
Attach this component to your Toggle gameobject and check out the Inspector:
using UnityEngine.EventSystems;
using UnityEngine;
using UnityEngine.UI;
public class InvertedToggleEvent : MonoBehaviour{
//wont show in inspector
//public UnityEngine.Events.UnityEvent<bool> toggleEvent;
//this will show in inspector
[System.Serializable]
public class UnityEventBool : UnityEngine.Events.UnityEvent<bool>{}
public UnityEventBool onValueChangedInverse;
private void Start()
{
GetComponent<Toggle>().onValueChanged.AddListener( (on)=>{ onValueChangedInverse.Invoke(!on); } );
}
}
3 Likes
How to do that without a script?
I believe nothing has changed since then, so you don’t.