Why is my serialized custom class/struct not properly instantiated (values return null/default) ?
Problem in a nutshell:
I have created a custom class/struct and made it [Serializable], then added [SerializeField] properties to access its field in the editor. In-editor this behaves as excepted, however on run properties referencing Objects either return null or the default value.
Code:
public class PlayerCollisionChecks : MonoBehaviour {
[Serializable] public struct RaycasterGroup { // Using class makes no difference
public CollisionReport collisionType;
public LayerMask collisionMask;
public Transform startPoint;
public Transform endPoint;
public RaycastDirection direction;
public float distance;
public int numberOfRays;
public bool showDebugRays;
}
[SerializeField] RaycasterGroup _firstGroup;
...
// returns
// start- & Endpoint -> always null
// CollisionType & Direction (custom Enums) -> always first(=default) value in grouping
// distance & number of rays (int) -> the value set in the editor (as expected)
}
As far as I can tell I'm following the guidelines outlined here. I've tried changing it to a class and using ISerializationCallbackReceiver, but that seems to prevent serialization the way I intend it (i.e. as dropdown menus, see picture).
Your answer
Follow this Question
Related Questions
Instantiate - Getting specific data from another script 0 Answers
How Can I Stop my Array Being Cleared on Runtime via Serialisation? 0 Answers
Custom classes behave like structs - Clarify 0 Answers
ISerializationCallbackReceiver - total confusion or simply unstable? 0 Answers
Unable to find custom classes in PUN2 (PhotonNetwork, PUNClasses) 0 Answers