問題
独自クラス側にSerializableを設定した上で
using System;
using UnityEngine;
[Serializable]
public class Training : MonoBehaviour
{
[SerializeField] private string title;
[SerializeField] private string myAnswer;
[SerializeField] private string resultTxt;
[SerializeField] private string resultImg;
[SerializeField] private string resultGif;
[SerializeField] private string time;
[SerializeField] private string correctOrWrong;
[SerializeField] private string studentId;
}
参照側で、SerializeFieldもちゃんと設定しても(図2)

Inspectorで値がシリアライズされない場合がある(図3)。

図4のようになっていて欲しかったのだが・・・

(・_____・)ああああああああああああああ

解決
UnityにおけるSerializeの仕組みには罠があって、まず前提としてMonoBehaviourを継承したスクリプトはパブリックフィールドのシリアライズ対象にならない(図6)。

なぜかというと、MonoBehaviourを継承したクラスは、Inspectorでオブジェクトにアタッチして使うことが想定されているためである。C++側のコードを見ていないのであくまで推測だが、”MonoBehaviour” クラスの継承元の “Object” のレベルで Unity独自のシリアライズ処理を施しているのではないかと思う。
そのため、クラスのフィールドをシリアライズしたい場合、MonoBehaviourの継承を消した上で参照を入れれば良い。
