Search Unity

Binary toggle box error

Discussion in 'Scripting' started by RyanBoomer30, May 24, 2020.

  1. RyanBoomer30

    RyanBoomer30

    Joined:
    May 24, 2020
    Posts:
    1
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class Toggle : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private int number;
    10.  
    11.     public event Action<int, bool> OnToggleChanged = delegate{};
    12.  
    13.     private void Awake()
    14.     {
    15.         GetComponent<Toggle>().isOn = false;
    16.         GetComponent<Toggle>().onValueChanged.AddListener(HandleToggleChanged);
    17.     }
    18.     private void HandleToggleChanged(bool enabled)
    19.     {
    20.         OnToggleChanged(number, enabled);
    21.     }
    22.     private void onValidate()
    23.     {
    24.         GetComponentInChildren<Text>().text = number.ToString();
    25.         gameObject.name = "Toggle" + number;
    26.     }
    27. }
    28.  
    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?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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!