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

Access a particular variable

Discussion in 'Scripting' started by fredyyanez, Jun 2, 2015.

  1. fredyyanez

    fredyyanez

    Joined:
    Jul 8, 2014
    Posts:
    33
    So I have a Widget buttons ( leap) and all of the buttons are running a script called button script. I want to access either a particular public variable for a particular button or call a function of that button that would return the particular variable. How can I do this?
    Because every script for all the buttons are called the same if i call
    scriptName.getValue();

    It would return the value of a random button. I want it to return the value of a particular one. Ive tried
    Using an SerializeField and input the button that way and then call the function that would return the variable i want but auto complete does not show that option.


    thanks,

    Fredy
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Get a handle to that particular button and do GetComponent to grab the button script. That way you have that objects button script.
     
  3. fredyyanez

    fredyyanez

    Joined:
    Jul 8, 2014
    Posts:
    33
    The variable I want to access is called "value", and the function that returns that value is called "getValue("). The button gameobject is called "button" , so I would called button.getComponent , then how would I reach the variable I need( value)?


    Thanks!
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Check out my signature and go to the misc. tutorials. I have a tutorial on GetComponent that comes with a packet you can download and try out some exercises.

    You access the script through the handle .

    myHandle(button).value = new value.