Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. public class ToggleActiveGameObject : MonoBehaviour
  2.     {
  3.         [SerializeField] private GameObject _targetGameObject;
  4.  
  5.         private Button _toggleButton;
  6.         private static UnityEvent _otherGameObjectActivated = new UnityEvent();
  7.         private static bool _anyActiveGameObject = false;
  8.  
  9.         private void Awake()
  10.         {
  11.             _toggleButton = GetComponent<Button>();
  12.             _toggleButton.onClick.AddListener(Toggle);
  13.             _otherGameObjectActivated.AddListener(DisableOtherGameObjects);
  14.         }
  15.  
  16.         private void Toggle()
  17.         {
  18.             bool myState = _targetGameObject.activeSelf;
  19.  
  20.             if (_anyActiveGameObject)
  21.             {
  22.                 _otherGameObjectActivated.Invoke();
  23.             }
  24.  
  25.             _targetGameObject.SetActive(!myState);
  26.  
  27.             if (_targetGameObject.activeSelf)
  28.             {
  29.                 _anyActiveGameObject = true;
  30.             }
  31.         }
  32.  
  33.         private void DisableOtherGameObjects()
  34.         {
  35.             _targetGameObject.SetActive(false);
  36.             _anyActiveGameObject = false;
  37.         }
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement