- Home /
Array of objectFiled in editorWindow
Hi I need to iterate through array of objectField to display them in editorWindow.
But, they are all null(duh), after initializing the array, so unity spills error of the null object reference variety.
Is there a way to iterate through the array and display them in the editor window without any fancy hacks? Or maybe change them from null to empty? Is that even a thing?
static Object[] source;
public static void Init(){
...
source = new Object[6];
...
}
public void OnGUI(){
for(int i = 0 ; i<6 ; i++){
// this gives errors:
source[i] = EditorGUI.ObjectField(new Rect(10,50*i,75,16),source[i],typeof(GameObject));
}
}
I'm just soo tired of all those unity driven hacks to everything unity based that my brain refuses to work anymore, even on such trivial things like this one. Please help.
Is there a way to iterate through the array and display them in the editor window without any fancy hacks?
What do you mean by this? Also, what error does it give you?
Answer by Mukabr · Jun 10, 2013 at 02:36 AM
well, i don't know how to do that. But, you can try doing this
public List<Object> yourList = new List<Object>(); //list of Objects
public List<string> yourList = new List<string>(); //list of Strings
public List<Float> yourList = new List<Float>(); //list of floats
*they need to be public variables so that you can see them in inspector.
In order to use Lists, you have to import System.Collections.Generic using this piece of code under your class definition:
using System.Collections.Generic;
Treat Lists as you would treat Arrays, but if you want to know the length of the list, use:
yourList.Count;
If you want to add an item:
yourList.Add(item)
*your item must be the same type of the List
if you want to remove an item:
yourList.Remove(item);
yourList.RemoveAt(index);
yourList.RemoveAll();
yourList.RemoveRange(startIndex, finalIndex);
Your answer
Follow this Question
Related Questions
why I Cant Add Multiple Objects to the "Object Field" in Editor Window? 3 Answers
Weird bug when dragging object onto EditorGUILayout.ObjectField 0 Answers
Custom scene view in editor window 2 Answers
Any way to attach a bit of code to individual UI Windows [Editor] 1 Answer
Create a custom editor window with repeating subsection (mockup included) 0 Answers