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 zardini123 · Jun 04, 2015 at 11:47 PM · errorarrayeditor-scriptingvariables

3D Class Array cleared on script save

I have a 3D class array DataPoint[,,] that is always cleared on change/save/recompile of scripts.

Parent script:

 using UnityEngine;
 using System.Collections;
 
 public class ParentScript : MonoBehaviour {
     
     public ChildScript[] terrainVoxels = new ChildScript[0];
 }

Child Script:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class ChildScript : System.Object {
 
     public DataPoint[,,] terrainData = new DataPoint[0,0,0];
 }

Data Point Script:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class DataPoint : System.Object {
 
     public int status;
 }

Inside the original version of the script, I define the ChildScript array correctly using editor scripts and I get no errors, and the array keeps its contents before/after script change. I also assign DataPoint array correctly and get no errors, but when I save/change a script after the contents are changed, array DataPoint[,,] terrainData is cleared completely. Is there any way to fix this?

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 Bunny83 · Jun 05, 2015 at 12:20 AM

Unity's serialization system doesn't support multi-dimensional arrays. You can use them at runtime without any problems, but whenever the containing instance is serialized the array will be missed out. So when the instance is deserialized the array will have it's default value which is in your case an empty array instance.

When Unity recompiles a script it has to completely destroy and shutdown the mono environment. Before Unity does that it serializes everything (including editor windows, inspectors, gameobject instances ...). Once the assemblies are recompiled they are loaded again and all objects are recreated and deserialized from the temporary serialized data to restore the old state.

This procedure can only survive objects which are serializable by Unity.

For more information see the documentation. Unfortunately they don't mention the dimension limit in the current documentation. Only one-dimensional arrays can be serialized. However you can create some sort of jagged array by using custom serializable classes:

 [System.Serializable]
 public class ChildScript 
 {
     public Dim1[] items;
 }
 
 [System.Serializable]
 public class Dim1
 {
     public Dim2[] items;
 }
 
 [System.Serializable]
 public class Dim2
 {
     public DataPoint[] items;
 }

It's quite ugly but this construct will be serializable by Unity. Fields which are serializable by Unity don't need to be initialized manually as the Inspector will automatically initialize the instances / arrays and you can change the item count in the inspector.

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 zardini123 · Jun 05, 2015 at 09:49 PM 0
Share

Are Array[][][]'s serialized?

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

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

Related Questions

objects missing from array, on prefab, after updating list name in script 1 Answer

Error: Unable to load the icon: 'CustomEditorWindow'. (Custom Scene View) 1 Answer

Im having an issue with a custom editor script pausing the whole editor with the little applicaton.message? 1 Answer

error CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `int' 1 Answer

When using input, array.Add replaces the last member 1 Answer


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