Search Unity

Class Serialization

Discussion in 'Scripting' started by iNoMore, Apr 16, 2015.

  1. iNoMore

    iNoMore

    Joined:
    Oct 13, 2013
    Posts:
    64
    Hello!!

    I have a small but annoing problem with Serialization...
    I have a class, for example, Node, that holds a refrense to the previous Node's class
    everything works fine except the fact that nothing is saved after I click play :\

    this is the class
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [System.Serializable]
    4. public class Node
    5. {
    6.     [SerializeField]public string Name;
    7.     [SerializeField]public Node Previous;
    8. }
    But!
    if I create something like this
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [System.Serializable]
    4. public class Node
    5. {
    6.     [SerializeField]public string Name;
    7.     [SerializeField]public Node2 Previous;
    8. }
    9.  
    10. [System.Serializable]public class Node2 : Node {}
    it is working :\

    If that matters, I am using Unity5, and I am using a custom editor to set the classes (I have tried to set as dirty but its still not working)

    Can anyone help?

    ** And on the same issue, does anyone knows a way to keep a referense to a class? kind of like a pointer, but something that I can serialize?
     
    Last edited: Apr 16, 2015
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You're most likely getting a serialisation warning in the log, which you should really stop ignoring ;)

    With regards to solving this shortcoming of the Unity serialisation system, see "I want to serialise something which Unity's serialiser doesn't support. What do I do?" in this post: http://blogs.unity3d.com/2014/06/24/serialization-in-unity/
     
  3. iNoMore

    iNoMore

    Joined:
    Oct 13, 2013
    Posts:
    64
    Yea well I figured it out...
    Its because the compiler is afraid of an endless loop of referenses, makes sense kind of
    So I just store a string ID, and on Start I search the nodes database for that ID (I have a dictinary<ID, NODE>)

    But thanks for the replay! and the link :)