• 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 Crazy Noa · Dec 06, 2013 at 05:19 PM · c#

Arrays suddenly not serializing?

While working on one of my projects today I came across something very, very weird. I tried to declare a public array of 100 bools, but would for some reason get an array with a length of 0. I tested it further with a few other datatypes, but neither of them gave different results. I tried opening one of my other projects but nothing changed. I also tried setting the size from the inspector instead, but it would reset to 0 when starting the game. Finally, I started a new project and this time it went back to normal. I could declare arrays and I could change their size from the inspector. However, as soon as I go back to the other two projects the problem resurfaces.

It might be worth noting that all arrays that are already in the script act like normal, so as long as I don't try to change something my scripts work just fine.

I'm still very much a novice programmer, but from the little I know it seems like the arrays don't serialize. As soon as I select a new object the size is forgotten. But there's absolutely no reason for that to happen. I'm working on a custom inspector for one of the projects, but the other one is completely vanilla and very, very simple.

This is the piece of code I used for testing, by either setting the length from the inspector or by adding "= new bool[100]".

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
     public bool Jump = true;
     protected Animator animator;
     public float Speed;
     public float JumpHeight;
     public ParticleSystem Particles1;
     public ParticleSystem Particles2;
     public bool [] Test = new bool[100]; 
 
     void Update () {
         print(Test.Length);
     }
 
 
     // Use this for initialization
     void Start () {
         animator = gameObject.GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         rigidbody2D.AddForce(Vector2.right * Input.GetAxis("Horizontal") * Speed);
 
         if (Input.GetAxis("Horizontal")>0)
             {
                 Vector2 temp = new Vector2(-1, transform.localScale.y);
                 transform.localScale = temp;
                 Particles1.enableEmission = false;
                 Particles2.enableEmission = true;
                 animator.SetBool("Walk", true);
             }
 
         if (Input.GetAxis("Horizontal")<0)
             {
                 Vector2 temp = new Vector2(1, transform.localScale.y);
                 transform.localScale = temp;
                 Particles1.enableEmission = true;
                 Particles2.enableEmission = false;
                 animator.SetBool("Walk", true);
             }
 
         if (Input.GetAxis("Horizontal") == 0)
             {
                 animator.SetBool("Walk", false);
             }
         
         if (Input.GetAxis("Fire1") > 0 && Jump == true)
         {
             rigidbody2D.AddForce(Vector2.up*JumpHeight);
             Jump = false;
         }
 
     }
 
     void OnCollisionEnter2D (Collision2D collision)
     {
             Jump = true;
     }
 }
 


I have restarted both my computer and software. All my code is written in C# using MonoDevelop. I'm on a Macbook Pro running Mavericks.

Comment

People who like this

0 Show 4
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 Sundar · Dec 06, 2013 at 05:25 PM 0
Share

Can you comment out //print(Test.Length); in Update() and type it in Start(). See what happens.

avatar image Crazy Noa · Dec 06, 2013 at 05:28 PM 0
Share

Still a zero, only difference is that it only prints it once.

avatar image Sundar · Dec 06, 2013 at 05:40 PM 0
Share

Give some couple of values at the inspector then run.

avatar image Crazy Noa · Dec 06, 2013 at 05:49 PM 0
Share

Seems to have worked this time. I tried setting a few values from code earlier, but it would just tell me that it was out of range.

After trying with a few values in an array of 90, it started reverting back to 90 instead of zero. If I set a few values it works like it's supposed to, but whenever the array is empty it goes back to 90.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Guts · Dec 08, 2013 at 04:23 AM

I ran some tests to see what happens in a few situations (Using Unity 4.3.1).

I used this simple script in the tests:

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour 
 {
     public bool[] Test = new bool[100];
 
     void Start() 
     {
         print(Test.Length);
     }
 }

Once you add that component to a GameObject, Unity serializes it and no longer cares if you change the size of the serialized array in the script (unless you click on the gear in the inspector and hit "Reset").

This behaviour is not exclusive to arrays. If you add another serialized field and initialize it as you do with the array, for example a public string field initialized to "Hello!", then once Unity serializes it it doesn't care if you change the string initialization to "Goodbye!", it's still going to have stored "Hello!" as the value. As this demonstrates if you attach it to an object and then play:

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour 
 {
     public bool[] Test = new bool[100];
     public string word = "Hello!";
 
     void Start () 
     {
         print(Test.Length);
         print(word);
     }
 }

I would consider this good and expected behaviour, as it's what makes editing per-instance values in the inspector possible. I guess maybe the thing to remember about serialized members is that you're turning them over to be managed by Unity and whoever is poking values into them in the inspector. If you want something more controlled you'll have to look elsewhere.

Comment
Bunny83

People who like this

1 Show 0 · 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

Answer by Crazy Noa · Dec 11, 2013 at 09:51 AM

The problem isn't that all arrays work differently than I'd expect them to. I have a script with ten or so arrays that work just fine. The thing is, when I declare a new one it does not behave like the others. For example:

 public string [] Dialogue = new string [100];

This one works just fine. It is one of the first arrays I declared in this script. I can get the length regardless of if I place the Print() in Update, OnGUI or Start. However, when I declare this one:

 public string [] Test = new string[100];

print(Test.Length) prints out 0. This is the exact same way I print out Dialogue, the only difference being that I switch out the name. And all my other arrays work just like Dialogue, it's just the ones I've declared since last friday that don't behave this way.

Might be worth mentioning that trying to use the array gives me IndexOutOfRangeException: Array index is out of range, so it's not that it fails to print the length.

Comment

People who like this

0 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 Bunny83 · Dec 11, 2013 at 11:39 AM 0
Share

This is not an answer! Please don't post such things as answer. You can post it as comment or edit your question.

Also like Guts said serialized arrays shouldn't be created by you. Unity will create the array automatically and you can change it's size and content in the inspector.

If you don't want the array to be serialized at edit time, make the array private or put a NonSerialized attribute on your variable

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Scripting and Coding Dictionary 0 Answers

Why Does My GameObject Not Spawn Anymore? 2 Answers

Camera Controls Need Help 1 Answer

Destroy Gameobject once 0 health 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