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

button.onClick Question

Discussion in 'Scripting' started by ajsnarr98, Jan 18, 2016.

  1. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    When a button is clicked, it can be set to call a public function of one of the components of an object given to it. This public function can also have one parameter, like an int, where when that function is selected in the inspector under the Button script, you must enter a parameter.

    Is it possible to change that parameter or the function selected through a script? I know there is a button.onClick aspect of buttons when scripting, but how would I change the function that is called when a button is clicked, and, more importantly, how would I change the argument passed to that function when the button is clicked? By default, that argument passed is 0 for and int (which is what I would need to pass to the function I am calling).

    Sorry about the confusing description, but it is the best I can do. I hope you can help :)
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You generally don't set a variable in the inspector. I would make a duplicate public function and have it call the other with the assigning variable.

    This is really dumb for doing grided data entry buttons. What you can do is simply create a script that will create the grided button and use the OnMouseDown method for the button object. This will achieve the same result.
     
  3. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    Alright thanks :)