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 /
avatar image
0
Question by ScopedGames · Feb 25, 2015 at 07:05 PM · c#serialization

How do I serialize a class?

Hello, I am trying to make a class of variables that are hidden (serialized). This is similar to the playermotor script that comes with unity. You have the little arrow to extend the field.

Also, there is no active/ non active check box next to the script however i don't know if this a problem or not.

How do I achieve this? So far I have this code but nothing shows up:

 using UnityEngine;
 using System.Collections;
 
 public class Script : MonoBehaviour {
 
     [System.Serializable]
     public class example
     {
         public float myFloat = 1;
     }
 }

Comment
Add comment · Show 2
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 ScopedGames · Feb 26, 2015 at 05:39 PM 0
Share

How do you actually make the reference then?

avatar image steakpinball · Feb 26, 2015 at 06:44 PM 0
Share

The checkbox only appears if the script will behave differently depending on the value of enabled. Start, Update, and FixedUpdate are some methods which make the tick appear. If you don't have those methods you don't need the tick.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by steakpinball · Feb 26, 2015 at 06:34 PM

Make a field of your serializable class in your script.

 [System.Serializable]  // This allows fields of this type to appear in the inspector
 public class Example {
   // These fields will become visible when the arrow is clicked
   public float myFloat = 0.1f;
 }
 
 // This is your script on the game object
 public class Script : MonoBehaviour {
   public Example field;
 }

Comment
Add comment · Show 1 · Share
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 ScopedGames · Feb 26, 2015 at 06:46 PM 0
Share

Thank you brianturner, now everything is working fine.

avatar image
0

Answer by loyum · Feb 25, 2015 at 07:24 PM

Hello, Do it like that:

 using UnityEngine;
 using System.Collections;
 using System.Xml.Serialization;
 using System.IO;
 public class Script : MonoBehaviour {
     public class Example
     {
         public float myFloat = 1;
     }
     public void serializeExample(Example example) {
         XmlSerializer serializer = new XmlSerializer(typeof(Example));
         TextWriter writer = new StreamWriter("saveFile.xml");
         serializer.Serialize(writer, example);
         writer.Close();
     }
 }


I didn't test it, but it should work :)

Good luck!

Comment
Add comment · Show 4 · Share
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 ScopedGames · Feb 26, 2015 at 07:48 AM 0
Share

loyum, when i do it the way you suggested, still nothing shows up in the inspector.

avatar image ScopedGames · Feb 26, 2015 at 07:53 AM 0
Share

Also, fafase's answer doesn't work. Please help me.

avatar image fafase · Feb 26, 2015 at 08:08 AM 0
Share

Are you actually creating an instance? Simply declaring does not do anything. You need to also add an instance reference.

What do you actually want? Concerning the tick for active/inactive, are you expecting that for your new class? Because this is only for $$anonymous$$onoBehaviour classes.

avatar image ScopedGames · Feb 26, 2015 at 05:43 PM 0
Share

So how do you make that reference?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform object issues 1 Answer

Bringing Down the Buttons 0 Answers

Custom assets give Missing (Mono Script) 0 Answers

OnSerializeNetworkView is not called 1 Answer

Assets Returning Null 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