Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Is there a simple way to use a bool like a radio button

Discussion in 'Scripting' started by RuanJacobz, Sep 29, 2015.

  1. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    I want to write a piece of code that makes sure only one bool at a time could be true.

    For example:

    Code (CSharp):
    1. IsWalking == true;
    2.  
    3. IsSitting = false;
    4. IsStandingStill = false;
    5. IsDoingAnythingElseReally= false;
    Of course if IsStandingStill where to become true, the rest would go false. Can anyone help me out here?

    UPDATE:

    Thank you guys, I managed to get enums to work and do the job!
     
    Last edited: Sep 29, 2015
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
  3. DuffyT

    DuffyT

    Joined:
    Dec 16, 2014
    Posts:
    37
    Make them all private, then make properties for all of them and in the setter put the rest false if put true. But if you're going to have more than let's say three or four (and don't yet know if there will be more) I would go with an enum.
     
    RuanJacobz likes this.
  4. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    Thank you guys, I'm going to go learn about enums