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

Using mutliple buttons for a common goal new UI system help

Discussion in '2D' started by jaekx, Oct 7, 2015.

  1. jaekx

    jaekx

    Joined:
    Dec 15, 2014
    Posts:
    27
    I need help! I'm not used to the new Unity UI system, as far as I know you can only add individual scripts to buttons. However, in my situation I want one script to handle the functionality of many buttons attached to an empty game object or something. Heres an image of what I want to try and program currently:



    For the current arrow buttons I want the word to change from skin -> hair - > eyes as pressed accordingly, how would I go about this with the new system? Make a global integer variable and use that in each button script??
     
  2. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    Hi! Attach the script to the canvas or other game object :D You can pass it a button specific variable too if needed!

     
    Last edited: Oct 8, 2015
    jaekx likes this.
  3. jaekx

    jaekx

    Joined:
    Dec 15, 2014
    Posts:
    27
    Hey, thanks for the reply but how do I access the buttons via code? I have to increment or decrement depending on the button being pressed up to 3 times. So instead of just changing the text once depending on the button if i hit right from state 1 itll go to state 2 if i hit left from state 2 itll go to state 1. I need a unifying variable between the 2 buttons keeping track of the integer state.
     
  4. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    I think you just solved the problem yourself ;) Maybe use a switch, or an array, or pass the name of the image you are displaying :D Just thoughts and ideas ^_^
     
    jaekx likes this.
  5. jaekx

    jaekx

    Joined:
    Dec 15, 2014
    Posts:
    27
    Im more asking related to syntax, I understand the logic, I know how to do this with the old OnGUI() system, i just dont know how to have a global variable between 2 buttons now that they each generally require a separate script... ya know what i mean? or am i just being stupid XD
     
  6. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    They don't require a separate script, but if you wanted to do it that way you still could :) To make a variable accessible in C# use:
    Code (CSharp):
    1. public String myString;
    2.  
    3. void Start(){
    4. }
    Or whatever Type you are using :D