Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Creating a Checklist / "Step-by-Step

Discussion in 'Scripting' started by santosluiz00, May 30, 2017.

  1. santosluiz00

    santosluiz00

    Joined:
    May 30, 2017
    Posts:
    7
    I'm making a checklist application.

    The operation is very simple. When you click in first Toggle, it'll be disabled. When you click in second Toggle, it'll be disabled, and so on...

    Like this:

    checklist.PNG



    Now, I've it:

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class AtivaBotao : MonoBehaviour{
    6.        
    7.     public Toggle checklist_toggle;
    8.     public bool playersReady;
    9.    
    10.     public void Start(){  
    11.         checklist_toggle = GameObject.Find("toggle1").GetComponent<Toggle>();
    12.     }
    13.  
    14.     public void EnableDisable(){      
    15.         if (checklist_toggle.interactable == true){
    16.             checklist_toggle.interactable = false;
    17.         }      
    18.     }
    19. }
    This code works with unique toggle, but I want to scale this. I want scale it for 3, 10, 50, 100 Toggles!

    How I can made it?

    I think refer the own Toggle, something like that: "this.GetComponent<Toggle>();".
    When I click in Toggle, the code will understand that I am clicking ONLY ON IT.

    I tried use the "this" but without successful.

    Resume:

    -> When I click in first toogle, it'll be disabled
    -> When I click in second toogle, it'll be disabled
    -> and so on...

    Anyone can help me? How I can update my code to make it?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,191
    You can have a script on each object that calls it's own method. Or you could have a manager script with a single method that gets called and takes a Toggle parameter

    Code (CSharp):
    1. public void EnableDisable(Toggle target){    
    2.         if (target.interactable){
    3.             target.interactable = false;
    4.         }    
    5.     }
    Then set that up in the onvaluechanged part of the toggle
     
    santosluiz00 likes this.
  3. santosluiz00

    santosluiz00

    Joined:
    May 30, 2017
    Posts:
    7
    Solved!! Thanks a lot for your help