- Home /
Custom Window array inside custom class
Hi, Unity Community.
I am trying to make custom window to my script. And the problem is displaying array in custom class of this script. I founded way to display arrays:
ScriptableObject target = this;
SerializedObject so = new SerializedObject(target);
SerializedProperty prop= so.FindPropertyRelative("prop");
but this didnt work in my case.
There is MonoBehaviour example:
public class Script: MonoBehaviour {
[System.Serializable]
public class CustomClass {
public GameObject[] goArray;
}
}
As you can see, there is GameObject array inside custom class of script. So, how to display 'goArray' in Custom Window?
Thanks for your attention!
Answer by Bunny83 · Nov 25, 2016 at 08:25 AM
I'm not sure what you mean by "CustomWindow". Do you mean inside an EditorWindow or inside a CustomEditor (which is a custom inspector)?
Apart from that this line makes not much sense:
ScriptableObject target = this;
That would mean you are inspecting a ScriptableObject and not your MonoBehaviour.
Furthermore your MonoBehaviour class named "Script" is actually empty. It doesn't has any variables. Your nested class "CustomClass" is just a class definition. It has no relation to it's containing class To actually have an instance of that class you need a variable inside the "Script" class. For example:
public class Script: MonoBehaviour
{
public CustomClass someVariable;
[System.Serializable]
public class CustomClass {
public GameObject[] goArray;
}
}
Now Script has someVariable which is an instance of your CustomClass. To use the SerializedObject to access the array in an editor script you would do something like:
SerializedObject so = new SerializedObject(yourScriptInstance);
SerializedProperty property = so.FindProperty("someVariable.goArray");
SerializedProperty elementAtIndex1 = property.GetArrayElementAtIndex(1);
Debug.Log("Name of the second element:" +elementAtIndex1.objectReferenceValue.name);
It's still not clear what you want to do but i hope this clears some questions you had.
Your answer
Follow this Question
Related Questions
DateTime, get all dates for the current week? 1 Answer
Custom Inspector GUI, Instantiating some classes 2 Answers
Mysterious crash involving an array 2 Answers
how to randomly pick a string from an array 3 Answers
C# Random String Array help 2 Answers