• 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 segment · Feb 25, 2014 at 09:03 AM · assetserializationassetdatabaseserializable

asset is emty on enering play mode

Hi, I have written a code where I parse xml file to List of items and create a serializable asset with that list. My problem is that when I enter a playmode my asset becomes empty. Do you have any ideas in why is that happening?

Thank you in advance.

Code:

 [Serializable]
 public class Day
 {
     public int Id { get; set; }
     public string Time { get; set; }
     public string Weather { get; set; }
     
     public List<GroupWave> GroupWaves { get; set; }
 }
 
 [Serializable]
 public class GroupWave
 {
     public List<Wave> Waves { get; set; }
 }
 
 [Serializable]
 public class Wave
 {
     public int At { get; set; }
     public int GenerateTime { get; set; }
     
     public List<Enemy> Enemies { get; set; }
 }
 
 [Serializable]
 public class Enemy
 {
     public string Id { get; set; }
     public string EnemyType { get; set; }
     public int Count { get; set; }
     public string Route { get; set; }
 }

And creating asset:

 public class ParseConfig : EditorWindow
 {
     //private const string xmlPath = "Assets/StreamingAssets/game_config.xml";
     private const string assetPath = "Assets/Resources/game_config.asset";
 
     [MenuItem("Zombie Sniper/Parse config")]
     public static void ParseAndSerializeData()
     {
         var days = DaysEntitiesParser.Parse(Application.streamingAssetsPath + "/game_config.xml");
         ScriptableObjectData data = ScriptableObject.CreateInstance<ScriptableObjectData>();
         data.SetData(days.ToArray());
 
         EditorUtility.SetDirty(data);
 
         AssetDatabase.DeleteAsset(assetPath);
         AssetDatabase.CreateAsset(data, assetPath);
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by segment · Feb 26, 2014 at 10:57 AM

Solution was to use [SerializeField] attribute:

 [System.Serializable]
 public class Day
 {
     [SerializeField]
     private int id;
     [SerializeField]
     private string time;
     [SerializeField]
     private string weather;
     [SerializeField]
     private GroupWave[] groupWaves;
 
     public int Id 
     { 
         get { return id; } 
         set { id = value; }
     }
     public string Time 
     { 
         get { return time; }
         set { time = value; }
     }
     public string Weather 
     { 
         get { return weather; }
         set { weather = value; }
     }
     public GroupWave[] GroupWaves 
     { 
         get { return groupWaves; }
         set { groupWaves = value; }
     }
 }
 
 [System.Serializable]
 public class GroupWave
 {
     [SerializeField]
     private Wave[] waves;
     
     public Wave[] Waves 
     { 
         get { return waves; }
         set { waves = value; }
     }
 }
 
 [System.Serializable]
 public class Wave
 {
     [SerializeField]
     private int at;
     [SerializeField]
     private int generateTime;
     [SerializeField]
     private Enemy[] enemies;
 
     public int At 
     { 
         get { return at; }
         set { at = value; }
     }
     public int GenerateTime 
     { 
         get { return generateTime; }
         set { generateTime = value; }
     }
     public Enemy[] Enemies 
     { 
         get { return enemies; }
         set { enemies = value; }
     }
 }
 
 [System.Serializable]
 public class Enemy
 {
     [SerializeField]
     private string id;
     [SerializeField]
     private string enemyType;
     [SerializeField]
     private int count;
     [SerializeField]
     private string route;
 
     public string Id 
     { 
         get { return id; }
         set { id = value; }
     }
     public string EnemyType 
     { 
         get { return enemyType; }
         set { enemyType = value; }
     }
     public int Count 
     { 
         get { return count; }
         set { count = value; } 
     }
     public string Route 
     { 
         get { return route; }
         set { route = value; }
     }
 }
Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"Save As..." Saving level assets at Runtime 0 Answers

AddObjectToAsset (reference) becomes null 1 Answer

Custom assets give Missing (Mono Script) 0 Answers

.asset file containing ScriptableObject is empty on openingUnity 1 Answer

Serializable class with access to its Monobehaviour host 2 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