Editor Script with for loop

So this is part of my code right now:

prefabamount = EditorGUILayout.Slider("Amount", prefabamount, 1, 99);
        GameObject[] worldfab = new GameObject[(int)prefabamount];
        int[] worldId = new int[(int)prefabamount];
        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

        for (int i = 0; i < (int)prefabamount; i++)
        {

            GUILayout.BeginVertical();
            worldfab _= EditorGUILayout.ObjectField("Prefab", worldfab*, typeof(GameObject), true) as GameObject;*_

worldId = EditorGUILayout.IntField(“ID”, worldId*);*
GUILayout.EndVertical();
}
EditorGUILayout.EndScrollView();
but everytime I change the prefab in the editor, it changes back to its null value. I think I know what the problem is, but I don’t know how to solve it. Does anybody have an idea?

This line:

GameObject[] worldfab = new GameObject[(int)prefabamount];

is wiping the array every time, setting all values to null.

What you can do instead is store the ‘worldfab’ variable outside the function as a member variable then when you need to adjust the size do this:

GameObject[] temp = new GameObject[worldfab.Length];
int count = Mathf.Min(temp.Length, (int)prefabamount);
for(int i = 0; i < count; i++)
{
    temp _= worldfab*;*_

}
worldfab = new GameObject[(int)prefabamount];
for(int i = 0; i < count; i++)
{
worldfab = temp*;*
}
You will need to do something similar with the int array.
You could always use a list instead then just add or remove entries as necessary.