Search Unity

Need help with my outfit buttons please.

Discussion in 'Scripting' started by cruising, Aug 15, 2015.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    I have made some outfits for my character, and now i want to make a basic "change outfit" script by pressing some canvas buttons to have a start to go further with.

    However, when i have 1 button function in the script for button0 and Dress0 it works as it should do, but when i add a function for button1 and Dress 1 it will just add both outfits on/off at same time on both buttons, any tips to prevent that?

    I have the actually script on the character and i have draged the character to each button and the function "ClickButton" is also correct on each button setting ( On Click() ).

    CODE:
    Code (CSharp):
    1. public class Outfit0 : MonoBehaviour {
    2.  
    3.     public Button Button0;
    4.     public Button Button1;
    5.     public Button Button2;
    6.     public Button Button3;
    7.     public Button Button4;
    8.     public Button Button5;
    9.  
    10.     public GameObject Dress0;
    11.     public GameObject Dress1;
    12.     public GameObject Dress2;
    13.     public GameObject Dress3;
    14.     public GameObject Dress4;
    15.     public GameObject Dress5;
    16.  
    17.     public void ClickButton(string buttonID)
    18.     {
    19.         if (Button0)
    20.             Dress0.active = !Dress0.active;
    21.  
    22.         if (Button1)
    23.             Dress1.active = !Dress1.active;
    24.     }
    25. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're not actually checking which button is being clicked here, just if it's not null. Assuming buttonID is the same as the GameObject's name, your code should look more like:

    Code (csharp):
    1.  
    2. if(Button0.name == buttonID)
    3.     Dress0.active = !Dress0.active;
    4. // etc
    5.