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
0
Question by Bokaii · Mar 15, 2015 at 04:01 PM · guifunctionintintegerarguments

UI system multiple ints as function arguments

Is there a way to have multiple integers as function arguments when using the UI Button?

I know you can split strings, but I need integers. As far as I know you can only have a single function argument to the button.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Kaz_Yamof · Mar 16, 2015 at 10:40 AM

Attach this in your button or make a referece to it on a empty game object on the scene. Then use the two ints x,y as your args.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TwoArgs : MonoBehaviour
 {
     //your button
     public Button b;
 
     //control the value to pass to event as you need
     public int x, y;
 
     void Start()
     {
         //register new event to onclick with the variables that control your args
         b.onClick.AddListener(() => CustomClick(x, y));
     }
 
     public void CustomClick(int a, int b)
     {
         //do something you want
         print(a + b);
     }
 }

Comment
Add comment · Show 8 · 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 Bokaii · Mar 16, 2015 at 10:45 AM 0
Share

See, this doesn't work. You cannot have 2 function arguments for the ui button.

avatar image Kaz_Yamof · Mar 16, 2015 at 10:47 AM 0
Share

Works pretty fine to me. On the UI Editor you will not see the args, but if you control it by code it works. alt text

imagem.jpg (24.3 kB)
avatar image Bokaii · Mar 16, 2015 at 03:40 PM 0
Share

So you're just making a new OnClick void?

avatar image Kaz_Yamof · Mar 16, 2015 at 03:49 PM 0
Share

$$anonymous$$ind of. It's void just on UI Editor. At execution time, the Custom Click event is added to onClick, and is triggered when you click. Just don't be so limited by the "UI way". It's all code behind, and use it is sometimes more useful.

avatar image Bokaii · Mar 16, 2015 at 03:50 PM 1
Share

Okay thank you :)

Show more comments
avatar image
0

Answer by Eric5h5 · Mar 15, 2015 at 04:08 PM

Make a class that contains multiple ints and use that for the argument.

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
0

Answer by Mmmpies · Mar 15, 2015 at 04:39 PM

Or you can actually add as many OnClick functions as you want, they all get called at the same time and just get update to do whatever you want after setting a bool to true e.g.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class MyButton : MonoBehaviour {
 
     public Text myText;
     private bool IsClicked = false;
     private int intOne;
     private int intTwo;
     private string stringOne;
     private string stringTwo;
 
 
     
     // Update is called once per frame
     void Update () {
         if(IsClicked)
         {
             myText.text = " = " + stringOne + " : " + stringTwo + " : " + intOne.ToString() + " : " +intTwo.ToString();
             IsClicked = false;
         }
     }
 
     public void StringOne(string String1)
     {
         stringOne = String1;
         Debug.Log (stringOne);
         IsClicked = true;
     }
 
     public void StringTwo(string String2)
     {
         stringTwo = String2;
     }
 
     public void IntOne(int Int1)
     {
         intOne = Int1;
     }
 
     public void IntTwo(int Int2)
     {
         intTwo = Int2;
     }
 }
 

Many-Button-Functions


manybutfunc.png (22.1 kB)
Comment
Add comment · Show 7 · 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 Bokaii · Mar 16, 2015 at 10:19 AM 0
Share

I just need 2 function arguments in the same OnClick. I need one for required level, and one for required mana.

avatar image Mmmpies · Mar 16, 2015 at 10:25 AM 0
Share

Ah O$$anonymous$$, really all you need is a list with spells in it. Then you just pass it which spell you want to cast and have the function check if the spell is within the players level ability and if the player has enough mana.

EDIT

In fact check the link on THIS YouTube video as it contains a Spell system. Will likely need editing for your use but it's a pretty good starting point.

avatar image fafase · Mar 16, 2015 at 10:28 AM 0
Share

That looks pushy when a basic class would do.

avatar image Mmmpies · Mar 16, 2015 at 10:38 AM 0
Share

Which bit @fafase? The original post about multiple onclicks or just sending the spell name or array number?

avatar image Bokaii · Mar 16, 2015 at 10:46 AM 0
Share

Its more like an unlocking system. Not the actual spell casting. Like an inventory, where you unlock the spells.

Show more comments
avatar image
0

Answer by marcus-fehse · Jan 26, 2018 at 08:39 AM

a bit late, i know @MrMelonPie, but for anybody else looking for a simple solution ...


      // call this variable and assign myInteger at OnClick() of the UI button
      public int myInteger { private get; set; }
      
      // call this function and assign myFloat at OnClick() of the UI button
      public void ButtonClickFunction(float myFloat)
      {
          DoSomethingWithMyParameters(myInteger, myFloat);
      }

should do it.

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

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

6 People are following this question.

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

Related Questions

Script not changing int variable 2 Answers

Checking to see if a loaded level is equal to a member of an integer array 1 Answer

Dropdown menu selection 0 Answers

A yield function for function Update? 1 Answer

How to set GUI button "1" to "0"? 1 Answer


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