EditorGUILayout.Foldout problem

I’m trying to create a foldout for each index in my array
i have it all working, the only problem is the foldout wont toggle between true and false?
i guess im trying to make something similar to a serialized array in the inspector?

Heres my code:

public bool[] showDialog;

public override void OnInspectorGUI () {

	showDialog = new bool[script.dialog.Count];

	for (int i = 0; i < script.dialog.Count; i++) {
		showDialog _= EditorGUILayout.Foldout(showDialog*, "Element"+i);*_

* }*

}

You can’t initialize the array in the GUI function, since its values will be reinitialized each time, hence not remembering when it is toggled to true. You need to initialize it elsewhere, or only once, or even add an expanded bool to each element you’re drawing.

Edit: e.g.

public class YourDialogLine 
{ 
  public string text; 
  public bool expanded; 
}

And then in the GUI function something like:

for (int i = 0; i < script.dialog.Count; i++) 
{
  script.dialog<em>.expanded = EditorGUILayout.Foldout(script.dialog_.expanded, script.dialog*.text);*_</em>

}