Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 02, 2013 at 04:33 AM by FWCorey for the following reason:

A comment steered me in the direction to fix it myself.

avatar image
0
Question by FWCorey · Apr 01, 2013 at 10:16 PM · serializedeserialize

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;
     }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 01, 2013 at 11:14 PM 2
Share

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 ;)

avatar image FWCorey · Apr 01, 2013 at 11:24 PM 0
Share

$$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. :(

avatar image Bunny83 · Apr 01, 2013 at 11:42 PM 1
Share

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.

avatar image FWCorey · Apr 02, 2013 at 12:02 AM 0
Share

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.

avatar image Bunny83 · Apr 02, 2013 at 12:50 AM 1
Share

So we can close the question?

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges