Home > Back-end >  How a Unity gameobject script check the value of toggle?
How a Unity gameobject script check the value of toggle?

Time:08-02

We are working on our school project and we are all beginners in Unity and C#.

There is a toggle group with three toggles on our UI panel and three cubes are on the screen. We are having trouble with writing scripts for these cubes. What we want is when the corresponding toggle is on, the color of the cube can be changed by clicking.

For example, when toggle 1 is on, we click cube1 and its color change.

For now, we know how to use the toggle to change color by scripts, but we are not sure how to write a cube script by checking whether the toggle is on or not to change the cube color using mouse clicking.

CodePudding user response:

You can check if a toggle is on or off by using .isOn

using UnityEngine;
using UnityEngine.UI;

public class CheckScript : MonoBehaviour
{
    public Toggle myToggle;

    void Start()
    {
     if(myToggle.isOn)
        {
            Debug.Log("Toggle is on");
        }
     else
        {
            Debug.Log("Toggle is off");
        }
    }

}

But I think what you actually need is this answer https://stackoverflow.com/a/57341924/19611934

  • Related