- Home /
Problem when instantiate button with Onclick.Addlistener
Hoi guys i've a problem when instantiate a new button prefab and add an onclick.addlistener, it doesn't work. If the button is already in scene work fine.
The error message ("ArgumentException: method arguments are incompatible, delegate etcc...")
Any solution?
Best regards
GameObject newButtonAmd = Instantiate(buttonPrefab) as GameObject;
newButtonAmd.transform.SetParent(GPUfolder);
newButtonAmd.GetComponentInChildren<Text>().text = "Amd";
newButtonAmd.GetComponent<Button>().onClick.AddListener(() => mymethod());
do you have a mymethod method expecting NO arguments? the code seems fine to me
add the code here:` public void BasketComponentCp(CaseBox state) {
switch (state)
{
case CaseBox.small:
Debug.Log("small");
break;
case CaseBox.middle:
GameObject newButtonNvidia = Instantiate(buttonPrefab) as GameObject;
newButtonNvidia.transform.SetParent(GPUfolder);
newButtonNvidia.GetComponentInChildren<Text>().text = "Nvidia";
newButtonNvidia.GetComponent<Button>().onClick.AddListener(() => AddGPUInstanceNvidia());
GameObject newButtonAmd = Instantiate(buttonPrefab) as GameObject;
newButtonAmd.transform.SetParent(GPUfolder);
newButtonAmd.GetComponentInChildren<Text>().text = "Amd";
newButtonAmd.GetComponent<Button>().onClick.AddListener(() => AddGPUInstanceAmd());
break;
case CaseBox.big:
Debug.Log("big");
break;
}
}
public void AddGPUInstanceAmd()
{
debug.log("dosomething") ;
}`
to me it looks ok, but can you try simply
newButtonAmd.GetComponent<Button>().onClick.AddListener(AddGPUInstanceAmd);
Your answer
Follow this Question
Related Questions
Mouse Methods (OnMouseDown, OnMouseOver etc) stop working after certain circumstances 0 Answers
How do I use a Toggle to activate and deactivate scripts? 1 Answer
PauseButton keeps getting null reference exception 1 Answer
button OnClick function triggering another buttons OnClick 0 Answers
[Solved] Button OnClick properties are missing after loading the scene 4 Answers