So since 4.6 unity uses a new UI system, what i never used. Until now.
What I'm trying to do is generating buttons in a dynamic way, thus I also have to add the onClick event in a dynamic way (or atleast through scripting).
I've tryed to extend the onClick Listener, but it doesn't want to work:
btn.GetComponent<Button>().onClick.AddListener(() => { placeBuilding(obj.name); });
It will give this error which does sounds pretty clear in what is wrong:
Assets/Scripts/Menu/btnBouwen.cs(72,45): error CS0119: Expression denotes a 'method group', where a 'variable', 'value' or 'type' was expected
However I have no idea how to use an UnityAction
as it seems the required type for the call.
Feels like I'm missing something really easy. Hope somebody can help me.
The problem i was facing is that my Button
was conflicting with a custom class i made called Button.cs
. Now i've learned not to use Class names that already exist in the environment^^
fixed it with the direct path to the Class:
UnityEngine.UI.Button btn = newButton.GetComponent<UnityEngine.UI.Button>();
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(() => placeBuilding(obj.name));
code:
GameObject button = Resources.Load <GameObject>("Button"); //loading from resource
GameObject newButton = Instantiate(button);
newButton.transform.parent = panel.transform;
newButton.GetComponentInChildren<Text>().text = obj.name;
newButton.transform.position = button.transform.position;
newButton.transform.position += new Vector3(20*x, -70 * z, 0);
UnityEngine.UI.Button btn = newButton.GetComponent<UnityEngine.UI.Button>();
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(() => placeBuilding(obj.name));
This code is inside a loop