Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class Toggle : MonoBehaviour { [SerializeField] private int number; public event Action<int, bool> OnToggleChanged = delegate{}; private void Awake() { GetComponent<Toggle>().isOn = false; GetComponent<Toggle>().onValueChanged.AddListener(HandleToggleChanged); } private void HandleToggleChanged(bool enabled) { OnToggleChanged(number, enabled); } private void onValidate() { GetComponentInChildren<Text>().text = number.ToString(); gameObject.name = "Toggle" + number; } } So I am trying to make a binary game that use the shortcuts 1,2,3,4,5,6,7,8 to select each toggle box and adding them up into a result. However I am running into a problem that the Unity default toggle under UI but I am getting an error that Toggle does not contain isOn, onValueChanged and text. What could be the problem here?
You sir are stealing... stealing I say! You have stolen the Toggle name for yourself! Your MonoBehavior class should be named something else besides Toggle really. Or if you must call your class Toggle, then use UnityEngine.UI.Toggle to get at the UI Component, and say so explicitly wherever you want it. I prefer to just rename my class to something like ToggleController Don't forget to rename both the class and the .cs file identically!