Reputation: 15
I just new to serialization, and have a question why the public array of custom class does not show up on inspector.
let's say I have code like follow
using UnityEngine;
using System.Collections;
public class A:MonoBehaviour{
public B b;
public C[] c= new C[3];
public D d;
public E[] e;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
[System.Serializable]
public class D
{
}
// Same goes for this you can remove monobehaviour
[System.Serializable]
public class E
{
}
I found that variable 'b' is on inspector in unity but I couldn't find where 'c' is.
https://docs.unity3d.com/ScriptReference/SerializeField.html
As far as I know, I don't need to do [Serializefield] or [Serializable] public variables both a and b to show up inspector(because both are public, must automatically show up on inspector(?)).
Can anyone explain to me why 'c' does not appear on the inspector? Or am I miss-understand something? Any explanation would be appreciated.
Here is B and C class. Same code just name difference.
using UnityEngine;
using System.Collections;
public class B: MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class C: MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I tried D,C suggested below answer. and both not shows up on the inspector.
Upvotes: 1
Views: 3283
Reputation: 1060
This isn't a problem that the question has, but just in case someone else runs into it like I did, here's another tip.
Make sure that the array field you would like to serialize is NOT a property. For whatever reason, it doesn't seem to like serializing properties.
public class A : MonoBehaviour
{
// works fine (so long as B meets the conditions mentioned by others)
public B[] SomeBees;
// does NOT work. do not do.
public B[] MoreBees { get; set; }
}
It appears that the reason properties don't serialize well is because they're literally not supported as stated here.
The serialization system can do the following:
- CAN serialize public non-static fields (of serializable types)
- CAN serialize nonpublic non-static fields marked with the SerializeField attribute.
- CANNOT serialize static fields.
- CANNOT serialize properties.
Upvotes: 0
Reputation: 195
// Attach this to gameobject and your classes will show up on inspector
public class A : MonoBehaviour
{
// both B and C is class
public B b;
public C[] c;
}
//This doesn't need to be monobehaviour as this should be the data class and A should be the container
[System.Serializable]
public class C {
public int number;
}
// Same goes for this you can remove monobehaviour
[System.Serializable]
public class B {
[SerializeField]
private int ID;
public string name;
}
Upvotes: 0
Reputation: 433
Derive your class A
from MonoBehaviour
and attach it to a GameObject in the inspector.
Upvotes: 0
Reputation: 4056
Serialization in your current case is about making the object "readable" for the unity's inspector.
When you apply a Serializable
attribute, it means you are saying that Objects of this type can be 'readed' by the unity's inpector.
When you apply a Serializefield
attribute, it means that you are making the current object 'readable' by the inspector.
(Provided that it can be serialized, and it wasn't already serialized in the first place)
Back to your issue, you should mark C
with a Serializable
Attribute in your case.
(Though most of the time I find that I don't need to)
Like so:
[System.Serializable]
public class C {}
Upvotes: 0