Search Unity

Exposed Array Values not working properly

Discussion in 'Immediate Mode GUI (IMGUI)' started by QFSW, Feb 7, 2016.

  1. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Hi all, I'm trying to expose an array by using a for loop
    Code (CSharp):
    1.  ExtraSceneCount = EditorGUILayout.IntField("Extra Scene Count", ExtraSceneCount);
    2.             Scenes = new string[ExtraSceneCount+1];
    3.             for (int i= 1; i<=ExtraSceneCount; i++)
    4.             {
    5.                 Scenes[i] = EditorGUILayout.TextField(" ", Scenes[i]);
    6.             }
    When i try to write into any of the text fields from the for loop, the text is cleared when i click on something else
    Anyone know why?
     
  2. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    I'm assuming this whole code bit is inside an OnGUI().
    so, what you are doing is resetting the Scenes array every iteration.
    You are the one that is actually clearing the text field with line 2.
    You can have unity handle the array for you, using serialized property. (see docs on SerializedProperty and SerializedObject classes.
     
  3. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Nvm, thank you but I had fixed it
    Thanks anyway :)