3

screenshot

I'm having an issue adding new values in fields after they are created in Editor, they keeping coming back and if i enter a new one it will display the first value. Can anyone give me snippet of code how can i add values which will be automatically saved.

Thanks!

    GUILayout.BeginHorizontal();
    GUILayout.BeginVertical();
    addInteger = GUILayout.Toggle(addInteger, "Integers");
    howMuchIntegers = EditorGUILayout.IntField(howMuchIntegers);
    intNames = new string[howMuchIntegers];
    if (addInteger)
    {
        if (howMuchIntegers != 0)
        {
            GUILayout.BeginVertical("box");
            for (int i = 0; i < howMuchIntegers; i++)
            {
                intNames[i] = i.ToString();
                intNames[i] = EditorGUILayout.TextField(intNames[i]);
            }
            GUILayout.BeginVertical("box");
        }
    }
    GUILayout.EndVertical();
    GUILayout.EndHorizontal();
6
  • What happens if you remove intNames[i] = i.ToString(); Commented May 4, 2018 at 13:28
  • Still won't alow me to update values it reverts them back to null as they are. Commented May 4, 2018 at 13:31
  • Second question. So you have this part intNames = new string[howMuchIntegers]; What happens if you remove this from the OnGui() section, and instead put it in just the class, or in the Start(). I think this part is getting called every update and is overwriting whatever you just saved. Commented May 4, 2018 at 13:33
  • That part works fine, look. imgur.com/a/RtcfypJ Commented May 4, 2018 at 13:36
  • I want to type the values in those fields and to be saved in the array intNames[]; Commented May 4, 2018 at 13:38

1 Answer 1

1

I think your data is getting overwritten in 2 places.

Here,

intNames = new string[howMuchIntegers];

and here,

intNames[i] = i.ToString();

So here is one solution. Basically I just suggest that you extract the parts that don't need to be run 60 times a second, you can do this however you want.

 bool firstTimeRun = true;

 void OnGUI()
 {    
    GUILayout.BeginHorizontal();
    GUILayout.BeginVertical();
    addInteger = GUILayout.Toggle(addInteger, "Integers");
    howMuchIntegers = EditorGUILayout.IntField(howMuchIntegers);

    if(firstTimeRun)
    {
        intNames = new string[howMuchIntegers];
        if (addInteger)
        {
            if (howMuchIntegers != 0)
            {
                GUILayout.BeginVertical("box");
                for (int i = 0; i < howMuchIntegers; i++)
                {
                    intNames[i] = i.ToString();
                    intNames[i] = EditorGUILayout.TextField(intNames[i]);
                }
                GUILayout.BeginVertical("box");
            }
        }

        firstTimeRun = false;
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
    }
    else
    {
        if (addInteger)
        {
            if (howMuchIntegers != 0)
            {
                GUILayout.BeginVertical("box");
                for (int i = 0; i < howMuchIntegers; i++)
                {
                    intNames[i] = EditorGUILayout.TextField(intNames[i]);
                }
                GUILayout.BeginVertical("box");
            }
        }
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
    }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Glad I could help.
You saved me a lot of stress! ^_^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.