- Home /
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.
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);
}
}
See, this doesn't work. You cannot have 2 function arguments for the ui button.
Works pretty fine to me. On the UI Editor you will not see the args, but if you control it by code it works.
$$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.
Answer by Eric5h5 · Mar 15, 2015 at 04:08 PM
Make a class that contains multiple ints and use that for the argument.
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;
}
}
I just need 2 function arguments in the same OnClick. I need one for required level, and one for required mana.
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.
Which bit @fafase? The original post about multiple onclicks or just sending the spell name or array number?
Its more like an unlocking system. Not the actual spell casting. Like an inventory, where you unlock the spells.
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.
Your answer
Follow this Question
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