- Home /
Question by
Klasmic · Sep 09, 2017 at 09:59 AM ·
c#buttonsfunctionsbutton trigger events
cant run function when button pressed
I am trying to make a button run a function when I click it but that function wont show up, yes I have the script with the function attached to a game object, but yet it still doesn't work, here's the script with the function
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CraftItem : MonoBehaviour {
public GameObject countholder;
public void Craftitem(int crafttype, int requiredtype, int requiredcount, int requiredamount){
requiredcount = countholder.GetComponent<Counts> ().Counters [requiredtype];
if (requiredcount >= requiredamount) {
countholder.GetComponent<Counts> ().Counters [requiredtype] -= requiredamount;
countholder.GetComponent<Counts> ().Counters [crafttype] += 1;
}
}
}
feesh.png
(4.2 kB)
Comment
Best Answer
Answer by chiragjksol · Sep 09, 2017 at 10:50 AM
@Klasmic You can not set this function on button press. because function hold multiple parameter. if you have more then 1 parameter in button press function then you can use like this.
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);
}
}
Your answer
Follow this Question
Related Questions
UI Button not calling function properly? 2 Answers
Button Action Triggering Multiple Times Per Click? 1 Answer
Unable to show button after an event 0 Answers
Multiple Cars not working 1 Answer