Search Unity

Understanding switch and case functions with Toggle group

Discussion in 'Scripting' started by satchell, Feb 18, 2020.

  1. satchell

    satchell

    Joined:
    Jul 2, 2014
    Posts:
    107
    Hello, with four toggles in a group and a ChangeToggle function in C# each toggle passes an assigned int. The switch and case seem to work properly changing an enum; although, debug will pass the last result and new result, two debug lines when I'm expecting just one.

    Are switch/case a good option compared to if's in this case? Is dual Debug something I can overlook or should I avoid this use?

    Toggle_01.PNG Toggle_02.PNG

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5.  
    6. public class Generic
    7. {
    8.     public enum ItemCategory
    9.     {
    10.         Item,
    11.         Pharmacy,
    12.         Packs,
    13.         Bank
    14.     }
    15. }
    16. public class EnumsAndToggles : MonoBehaviour
    17. {
    18.     public Generic.ItemCategory section;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         ChangeItems(0);
    24.     }
    25.  
    26.     public void ChangeItems(int toggle)
    27.     {
    28.         switch (toggle)
    29.         {
    30.             case 1:
    31.                 section = Generic.ItemCategory.Pharmacy;
    32.                 break;
    33.             case 2:
    34.                 section = Generic.ItemCategory.Packs;
    35.                 break;
    36.             case 3:
    37.                 section = Generic.ItemCategory.Bank;
    38.                 break;
    39.             default:
    40.                 section = Generic.ItemCategory.Item;
    41.                 break;
    42.         }
    43.         Debug.Log(section + " Section");
    44.     }
    45. }
    Thanks-
     
    Last edited: Feb 19, 2020