- Home /
A comment steered me in the direction to fix it myself.
Class not deserializing properly
I'm having a problem with one of my classes where instantiated prefabs that contain components with variables of the class type are initializing with a value of zero and not getting their values from the source prefabs. I'll attach the class here but it is fairly long due to a lot of operator overloading, implicit conversions and implementation of both ISerializable and IConvertible interfaces.
/// <summary>
/// Container class for a float variable with a clamped range. Also handy for minimum/maximum checks to variable parameters.
/// </summary>
[Serializable]
public class FloatRange : ISerializable, IConvertible
{
public float currentValue;
public float minimumValue;
public float maximumValue;
public FloatRange(){}
/// <summary>
/// Check if a value is within range
/// </summary>
/// <param name="value">The float to check</param>
/// <returns>True if between min and max inclusive.</returns>
public bool Contains(object value)
{
float testvalue = Convert.ToSingle(value);
if (testvalue >= MinimumValue && testvalue <= MaximumValue)
return true;
else
return false;
}
}
Example using it:
public class DummyEnemy : MonoBehaviour
{
public FloatRange ragdollOnHitpointRange;
public override void ApplyDamage(float damage)
{
if (damage < 0) damage = -damage;
if (rigidbody && ragdollOnHitpointRange.Contains(damage))
rigidbody.isKinematic = false;
}
}
99% of your code is completely irrelevant for Unity's serialization system. Properties aren't used at all by Unity's serializer. Unity directly serializes your public fields or private fields when they have the SerializeField attribute. The class itself needs the Serializable attribute. That's all. Unity doesn't use the default .NET / C# serialization system.
Unity will create an instance of your class automatically with the default constructor. Once the whole object / component is "created" Unity will deserialize the values.
I'm not sure what's your exact problem since all we see is your custom class with 99% of irrelevant code ;)
$$anonymous$$ost of the time I see the first comment on a question is to post all of the code so I copied the whole class ins$$anonymous$$d of just bits of it. I also didn't know if maybe one of the interface members was interfering and figured better safe than sorry. Even changing the three private variables to public, Unity just refuses to give me a clone with anything but 0 values. I'm completely stumped.
Okay I tried putting in a default constructor (It didn't have one as you can see) and it had no effect. :(
Can you add an example where and how you use this class? (and remove all the operators and properties ;) )
Btw: SerializeField is not for "inspector support", it tells the serializer to serialize this field (which will in addition make it visible in the inspector). This behaviour hasn't changed in Unity4 at all. A propertydrawer is an editor class and completely optional. It only affects how the field will be displayed in the inspector.
Okay I've edited the class code down to something manageable and did the same for a $$anonymous$$onoBehaviour that uses it on a prefab.
I closed Unity and restarted it and everything magically works again. Thanks for your help though. I'm thinking it was the missing empty constructor. I didn't realize Unity reinvented the serialization wheel. :P I have all the other stuff in there because this class is Serialized and Deserialized for file IO and stream processing. The other stuff was so I could use it as a drop in replacement for float with automatically enforced bounds.
Follow this Question
Related Questions
Serialize/deserialize on ASP.NET (c#) server - possible? 4 Answers
Need Help Serializing/Deserializing XML 0 Answers
Problem sending serialized data with Play Games Platform 0 Answers
Unity C# Deserialization dealing with extra properties 1 Answer
Passing lists to serialize and deserialize methods for XML parsing 0 Answers