How do I get a List or array in a custom inspector?
Hey guys,
I have a Serializable class that has a custom inspector and I need an array or List of it in another class.
This is the class that has a custom inspector, it's much longer, but I've taken a sample to make it easier...
using UnityEngine;
using System.Collections;
[System.Serializable]
public class StepCheck : MonoBehaviour
{
public string vsCharacter;
public int characterBlade;
public bool canLose;
public int stepOnLose;
public int stepOnWin;
}
And this is the class that needs to contain a list...
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Scene : MonoBehaviour
{
public List<StepCheck> steps;
--or--
public StepCheck[] steps;
}
I've searched through google and can only find one good answer that dates back to 2013 which doesn't seem to work for whatever reason.
So far, this is all that happens....
And this is what should be in each of the elements...
I can't work this out for the life of me, does anyone have any suggestions?
Cheers in advance!
Answer by Gnorpelzwerg · Dec 29, 2015 at 09:20 AM
It looks like your Element 0 is just not assigned. Do you need to inherit from MonoBehaviour in the StepCheck class? I would recommend you to define it inside the Scene script, if you don't need the StepCheck class outside of it. Displaying a class structure inside a public Collection works fine for me then. Also there is no need to add the [System.Serializable] Attribute to MonoBehaviour classes since Unity displays them automatically. I used this in one of my projects to handle a time controlled in-game console. The class was shown in the editor inside the list. Which was admittedly added at runtime.
public class Log : MonoBehaviour {
public List< Message > _messages = new List< Message >( );
[System.Serializable ] public class Message {
public Message( string text, float deathtime ) {
_text = text;
_deathtime = deathtime;
}
public string _text;
public float _deathtime;
}
...
Thanks for the info! $$anonymous$$anaged to get it working as intended, cheers again :)
When i just tried to derive class $$anonymous$$essage from $$anonymous$$onoBehaviour, i got the same issue. The list is not empty, but classes deriving from $$anonymous$$onoBehaviour seem unable to display in the inspector view. Are you adding the StepCheck class instances by searching for a script on other GameObjects with GetComponent( )? If so, I would rename the StepCheck script (if needed as $$anonymous$$onoBehaviour) and define a class without inheritance from $$anonymous$$onoBehaviour inside it as public variable, that contains all the variables you need to display in the inspector.
Your answer
![](https://koobas.hobune.stream/wayback/20220612054207im_/https://answers.unity.com/themes/thub/images/avi.jpg)