• 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
Question by Tuck · Apr 02, 2011 at 11:07 PM · arraydestroyeditor-scriptingserialization

Keeping object between edit and play modes.

I have an object with a multi-dimensional array of game objects that is initialized and filled in a function called from a custom inspector during edit mode. As soon as I press play the object is destroyed and rebuilt, which makes the array uninitialized/empty again.

I call 'DontDestroyOnLoad(this.gameObject)' in the object's awake function, but this doesn't seem to be doing anything. How can I preserve this array between edit and play modes? Thanks.

Edit: Here is the initialization for the array I'm trying to keep around. The 'fillArray()' function is called on a button press from my custom inspector. I noticed no difference after adding [System.Serializable] and [SerializeField]

using UnityEngine; using System.Collections; using System.Collections.Generic;

[System.Serializable] public class ArrayTest : MonoBehaviour { //Array size variables int iLength, iWidth, iHeight;

 //A 3D array of game objects to be filled
 [SerializeField]
 GameObject[,,] goPrefabs;

 //Prefab of game object to put in 3D array
 public GameObject goPrefab; 

 public void fillArray()
 {
     iLength = (int)(transform.localScale.x);
     iWidth = (int)(transform.localScale.z);
     iHeight = (int)(transform.localScale.y);
     goPrefabs = new GameObject[iLength, iHeight, iWidth];

     //Fill the array with game objects
     for(int i = 0; i < iLength; i++)
     {
         for(int j = 0; j < iHeight; j++)
         {
             for(int k = 0; k < iWidth; k++)
             {
                 GameObject goCurrentObject = Instantiate(goPrefab) as GameObject;

                 goPrefabs[i, j, k] = goCurrentObject;
             }
         }
     }
 }

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Jesse Anders · Apr 03, 2011 at 06:28 AM

Make sure the array is serializable (either public, or marked with the SerializeField attribute). You might also need to mark the object as 'dirty' from the editor script, although I can never remember exactly how that's supposed to work.

If that doesn't help, maybe you could post the variable declaration for the array.

Comment
Tuck

People who like this

0 Show 5 · 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 Tuck · Apr 03, 2011 at 04:41 PM 0
Share

I added the System.Serializable and SerializeField keywords to no effect. I'll post some code up to better illustrate what I'm talking about.

avatar image Jesse Anders · Apr 03, 2011 at 06:00 PM 0
Share

Is 'goPrefabs' the array you're wanting to serialize? I'm not sure if Unity directly supports serialization of multi-dimensional arrays. (There are workarounds though - for example, you can store the data as a 1-d array and then interpret it as a 2-d array.)

avatar image Tuck · Apr 03, 2011 at 09:45 PM 0
Share

Yes goPrefabs is the array I want to serialize. Would you mind going into more detail about the workaround you mentioned? It sounds like exactly what I need. Thanks!

avatar image Jesse Anders · Apr 04, 2011 at 06:21 AM 0
Share

There's more than one way to do it (you might find some examples if you search the forums for e.g. 'serialize 2-d array'). But, one method is to write a wrapper function to index a 1-d array as a 2-d array. Generally, converting 2-d indices to a single 1-d index will look something like this: index = row * num_columns + column (or the other way around, depending on your storage preference). Another approach that might work would be to create an array of serializable objects, each of which contains one row or column of the array and is itself serializable.

avatar image Tuck · Apr 09, 2011 at 04:21 AM 0
Share

Ha I have interpreted my 3D array as a 1D array and as a result all of the array items are serialized but the array itself is still destroyed. I'll kick around the forums for another solution. Thanks for all the help!

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

No one has followed this question yet.

Related Questions

Destroy not destroying in time 1 Answer

Remove an object from an array and destroy it (C#) 2 Answers

How to use custom file types as Unity assets 1 Answer

SerializedProperty with children class fields 2 Answers

Trouble setting the object reference in a property drawer 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