Quantcast
Channel: Latest Questions by betaFlux
Viewing all articles
Browse latest Browse all 30

UnityEditor GenericMenu - Problem with 'AddItem' in Foreach Loop

0
0
I'm working on a custom inspector and I'm trying to populate a GenericMenu with the content of a string array like this: public override void OnInspectorGUI() { if(GUILayout.Button("Add Type")) { var menu = new GenericMenu(); foreach(string s in typeNames) { menu.AddItem(new GUIContent(s), false, () => Debug.Log("s = " + s)); } menu.ShowAsContext(); Event.current.Use(); } } which works perfectly fine. The menu gets populated with all the different strings. The problem is that, no matter what option I click on the generic menu later on, the Debug.Log() always prints the first element from the array. Can someone explain to me, why GUIContent(s) works, but the lambda expression doesn't?

Viewing all articles
Browse latest Browse all 30

Latest Images

Trending Articles





Latest Images