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
1
Question by Matthias1231 · Mar 16, 2012 at 06:12 AM · assetsvariablespipelineassetpostprocessor

Why are certain script variable values set in the AssetPostprocessor being lost?

I have a script attached to a GameObject. The simplified script code looks like this:

 public class MyScript : MonoBehaviour 
 {
     public int       int_test         = 0;
     public int[]     intarray_test    = null;
     public ArrayList arraylist_test   = null;
     public float     float_test       = 0;
     public object    object_test      = null;
     public object[]  objectarray_test = null;
     public string    string_test      = null;
 
     void Start () {}
     void Update () {}
 }

On asset import, I assign data to the variables in the script objects like this:

 public class MyAssetProcessor : AssetPostprocessor 
 {
     void OnPostprocessModel(GameObject g)
     {
         Object[] scripts = GameObject.FindObjectsOfType( typeof( MyScript ) );
     
         foreach( MyScript script in scripts )
         {
             script.int_test         = 1234567;
             script.intarray_test    = new int[2];
             script.arraylist_test   = new ArrayList();
             script.float_test       = 0.1234567f;
             script.object_test      = "object";
             script.objectarray_test = new object[2];
             script.string_test      = "string";
         }
     }
 }

Once I start the game and hit a breakpoint in the Start function of MyScript, only some variables kept their values and some got lost:

alt text

Looks like only value types, other than the string, survived. Why?

Thanks!

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
1
Best Answer

Answer by syclamoth · Mar 16, 2012 at 06:25 AM

The key point here is that some of these types are compatible with Unity's inbuilt serialization, and others are not.

You may have noticed the correlation between the variables which show up in the inspector, and the variables which are getting saved here. As you can see, the only types that lose their values are the ones that don't show up in the inspector!

This is no coincidence. Because of the way Unity stores / restores objects in the Start step, the object will get destroyed and then rebuilt from the serialized data- and so, you lose any information that can't be saved.

If you absolutely need those types, I recommend that you write a serializable wrapper for them, that Unity can use to store the information required for them. I've done similar things for jagged/multidimensional arrays in the past (these are another kind of object that Unity can't serialize).

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 Matthias1231 · Mar 16, 2012 at 12:14 PM 0
Share

Thank you! I've been wondering what Unity is doing with the object. The pass through the serialization process brings sense to this see$$anonymous$$g oddity. Great observation on the object type not showing up, I didn't even notice that.

Upon further research I was able to solve my problem and this post helped as well as I need nested arrays: http://answers.unity3d.com/questions/35361/array-of-arrays-no-serialization.html

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

RTS Style Selection system (How to control one unit and not the others) 2 Answers

Getting an object from another script... 1 Answer

Trying to access a variable from another script attached to same object. 1 Answer

Saving data 1 Answer

One script for all variables? 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