Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by marf · Sep 04, 2014 at 08:40 PM · uiguibuttonmultipleparameters

UI Button multiple parameters

Hi, assuming I'm using Unity 4.6 and the new GUI system; is there a way to assign multiple parameters to a function called during the OnClick() event of a button?

I mean through the inspector, becuase I managed a solution using this code:

 btn.onClick.AddListener(() => { Login(username.text, password.text);  });

But I was wondering if there's was a simple way (obtain the same result using only the inspector).

Thank you,

Marco

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Kiwasi · Dec 27, 2014 at 10:07 AM

You can pass anything that inherits from Unity's object class as a parameter for the OnClick events. So simply create an object that inherits from ScriptableObject or MonoBehaviour, add multiple fields to contain the required data, and pass that through your OnClick event in the inspector.

Disclaimer: I haven't tried this yet, but according to the docs it should work.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fornoreason1000 · Dec 27, 2014 at 10:10 AM 0
Share

I can confirm that it works

avatar image TooManySugar · Oct 24, 2016 at 03:44 PM 0
Share

It does work! thank you!! this is a GREAT solution! You can pass as many as you whant of any type. YAY!

avatar image MrLucid72 · Jun 17, 2018 at 01:14 PM 0
Share

Can you provide an example? I'm sure this may differ in 2018

avatar image
3

Answer by RTSeegrist · Dec 27, 2014 at 09:31 AM

You can pass multiple integers by passing the numbers in a string then use .Substring() and int.Parse to convert the string of numbers to individual integers. If you pass the string "277830", the function below will convert the string to playerposition = 27, playerhitpoints = 78, and playerdamage = 30.

 public void playerstats (string stats){ 
  string temp = stats.Substring(0,2);
  playerposition = int.Parse(temp);
  temp = stats.Substring(2,2);
  playerhitpoints = int.Parse(temp);
  temp = stats.Substring(4,2);
  playerdamage = int.Parse(temp);
  }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

Answer by animepauly · Sep 06, 2014 at 09:25 PM

I think a much simpler version would be to just put those values in an array. That's what I am doing for my login code and it works well.

I have two input fields, I create an array with length of 2 and put the values of my text in that. I then return that array and pass that in as my parameter when I send the info on button click. So you could do something like this. (Note: I didn't test this exact bit so you might need to create an array variable and set the value to get user data like string[] arr = getUserData(), and pass that into the listener instead.

 private string[] getUserData()
 {
     InputField[] __inputFieldArr = GetComponentsInChildren<InputField>();
     string[] __userDataArr = new string[2];
     __userDataArr[0] = __inputFieldArr[0].text.text;
     __userDataArr[1] = __inputFieldArr[1].text.text;
 
     return __userDataArr;
 }
 
 btn.onClick.AddListener(() => { Login(getUserData());  });
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image marf · Sep 09, 2014 at 10:15 PM 0
Share

Yeah but of them are working solutions, but I was searching an inspector solution... Thank you for the replies, anyway :)

avatar image
0

Answer by jokim · Sep 04, 2014 at 08:44 PM

Not without some kind of extension or plugin, i'm afraid.

What you can do tho, is have 2 functions, which actually just stores those parameters, then use a third one to use those parameters...

Here's what I have in mind :

Have a script with 3 functions like so :

 string username;
 string password;
 
 public void StoreUser(string _username)
 {
   username = _username;
 }
 
 public void StorePassword(string _password)
 {
    password = _password;
 }
 
 public void Login()
 {
     //login with Username and password.
 }

and in the button, have it call the three functions. one after the other.

I am aware, this is far from optimal, but it's a workaround.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Dec 29, 2014 at 04:04 AM 0
Share

This could fail with hard to spot bugs if the underlying order of events changes. Events are currently called in order, but this is an implementation detail, not a specification. It could change in the future.

avatar image
0

Answer by marcus-fehse · Jan 26, 2018 at 07:45 AM

@marf @jokim because the "problem" is still the same - after years ... "Avoid making all properties public, because it's just a free for all and how spaghetti code is cooked up." - to quote john stejskal about getters, setters and auto properties in C# - brought me to this solution:


make the username variable accessible in editor

 public string username { private get; set; }  // not visible without these properties   

and assign string username in the OnClick() section of your button first. you may pass as many different types of variables to your function - step by step of course. ... and at the end you call Login(string) passing the password as last parameter like

 public void Login(string password)
    {
       LoginFunction(username, password);
    }   

that's it. less lines of code, less functions to call.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Button onClick events will not be triggered 1 Answer

UI Button and Color Tint Transition issue 0 Answers

Changing the target of a button that takes a gameobject as a parameter? 0 Answers

The Type Or Namespace UI could not be found - Trying to create a clickable button 2 Answers

How to draw GUI texture under UI elements? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges