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 IgorAherne · Jul 17, 2013 at 10:30 PM · editorarrayvariableplayreset

[Simple yet problematic] Array looses all the variables when I hit "Play"

Hello, dear Community! I need your help :'<

I need to set up an array through a custom Editor; I store array objects inside of this array. However, the base array looses all inside objects once I hit "play". This can be observed using EditorGUILayout.IntField that I use in OnInspectorGUI() function
How can this be solved?

I am using two scripts,

Thank you very much!

//ArrayScript.js

 #pragma strict
 
 var branchArray : Array = new Array();




//ArrayScriptEditor.js

  #pragma strict
 
 @CustomEditor(ArrayScript)
 
 class ArrayScriptEditor extends Editor{
 
     var _TaAS : ArrayScript; //(target as ArrayScript shortcut)
     
     
     function OnEnable(){
         _TaAS = (target as ArrayScript); //set up the shortut
     
     }
 
 
 
     function OnInspectorGUI(){    
         GUILayout.BeginHorizontal();
         GUILayout.Label("# of cells in branchArray");
         EditorGUILayout.IntField(_TaAS.branchArray.length, GUILayout.Width(40));
         GUILayout.EndHorizontal();
         
         if(GUILayout.Button("push an array into the branchArray")){
             var branch : Array = new Array();
             
             _TaAS.branchArray.Push(branch); //I am pushing an array object into the other array
         }
     
     }
 
 }
 
Comment
Add comment · Show 1
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 Jamora · Jul 19, 2013 at 06:22 PM 0
Share

This seems like a serialization issue. Read http://blogs.unity3d.com/2012/10/25/unity-serialization/ and see if you get any ideas. I also have a feeling a list of lists might not be serializable. You could try using a different, more easily serializable, data structure.

2 Replies

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

Answer by AntiLunchBox · Jul 19, 2013 at 06:33 PM

This is most definitely a serialization issue.

Check out the link.

Also, you want to have something like this in OnEnable(): (this is in c# so you have to convert to js)

 mObject = new SerializedObject(target);

And in your OnInspectorGUI() have:

 mObject.Update();

in the beginning and

 mObject.ApplyModifiedProperties();

at the end.

I haven't done editor scripts in js, but it should be about the same concept.

Comment
Add comment · Show 2 · 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 IgorAherne · Jul 19, 2013 at 06:41 PM 0
Share

The article is exactly what I was looking for, thanks so much!

avatar image IgorAherne · Jul 21, 2013 at 07:14 PM 0
Share

Switching for c#, now I am know 2 languages :D

avatar image
0

Answer by Owen-Reynolds · Jul 18, 2013 at 01:20 AM

Wouldn't branchArray = new Array(); destroy whatever was in it, when run? A regular script with a public-filled-in array just uses the array -- no initialization needed. I'd think this would be the same way. Get rid of that line.

Comment
Add comment · Show 3 · 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 IgorAherne · Jul 18, 2013 at 07:43 AM 0
Share

Still not working :<

my ArrayScript is now

//ArrayScript.js

 #pragma strict
 var initialized : boolean = false;
 
 var branchArray : Array;



and for the editor I added a little check OnEnable, so it looks like this:

//taken from ArrayScriptEditor

 function OnEnable(){
 
         _TaAS = (target as ArrayScript); //set up the shortcut
         
         if(_TaAS.initialized == false){
             _TaAS.initialized = true;
             _TaAS.branchArray = new Array();
          }
 }
     
 
 
avatar image IgorAherne · Jul 18, 2013 at 07:44 AM 0
Share

Hitting play still erases everything, although I initialize the Array only from the editor and only once

avatar image Owen-Reynolds · Jul 18, 2013 at 01:58 PM 0
Share

That looks a lot what worked for me (last seen over a year ago.) Except I used if(branchArray==null) ins$$anonymous$$d of the extra bool (using a C# [] array.)

I'm wondering whether the editor script is somehow making and displaying a "ghost" branchArray. You wouldn't know, since the only way to check uses the editorScript. Also, I avoid javascript, so JS style new Array(); could require some tricks which I'm completely ignorant of.

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

18 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

Related Questions

Custom editor window resets array 1 Answer

How can I make variables reset when leaving play mode? 3 Answers

Variables in editor script reset when playing 1 Answer

Is there a Function Callback for when a vairable is altered in the Inspector? 1 Answer

Inspector variables from class in List are always initialized to its default values 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