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 Ziboo · May 10, 2011 at 06:27 PM · javascripteditorarray

New to Unity, EditorScript not saving my Array

Hi everyone,

I'm trying to make a tool, I'm king a new to Unity, so I don't know if I'm doing all good:

My GeoPainterEditor.js is where I do everything.

And GeoPainter.js is my MonoBehevior script to store my data, and maybe in the future do some stuff.

I have to variables in GeoPainter.js:

var myGroups : Array = new Array();
var nbrGroupsCreated:int;

I'm modifing this variables from my GeoPainterEditor.

When I close the editor and reopen "nbrGroupsCreated" was saved, but not "myGroups" array.

I know why, cause I'm creating a new array each time via: "new Array()"

But I didn't find a way to only create ONCE and ONLY ONCE this array, and not each time I restart the editor ??

Here is my code:

GeoPainterEditor.js:

@CustomEditor(GeoPainter)

class GeoPainterEditor extends Editor {

private var isPainting = false; private var myDistance = 0.1; private var groupSelIndex : int = 0;

function OnInspectorGUI () {

 EditorGUILayout.BeginHorizontal();
 GUILayout.Label("Add/Remove Groups");
 if(target.myGroups.length != 0 && GUILayout.Button ("-")) {
     var index = (groupSelIndex -1);
     var go = target.myGroups[index];
     target.myGroups.RemoveAt(index);
     DestroyImmediate(go);

 }
 if(GUILayout.Button ("+")) {
     go = new GameObject ("GeoPainter_Group_" + target.nbrGroupsCreated.ToString());
     target.nbrGroupsCreated = target.nbrGroupsCreated + 1;
     target.myGroups.Add(go);
 }
 EditorGUILayout.EndHorizontal();
 var nbrTemp = 1;
 for(var obj in target.myGroups)
 {
     EditorGUILayout.BeginHorizontal();
     GUILayout.Label("" + nbrTemp + ": ");
     obj = EditorGUILayout.ObjectField(obj, GameObject);
     nbrTemp++;
     EditorGUILayout.EndHorizontal();
 }


 if(target.myGroups.length > 0)
 {
     EditorGUILayout.BeginHorizontal();
     GUILayout.Label("Selected Group: ");
     groupSelIndex = EditorGUILayout.IntSlider(groupSelIndex, 1, target.myGroups.length);
     EditorGUILayout.EndHorizontal();
 }

 if(GUILayout.Button("Start Painting"))
 {

 }
 myDistance = EditorGUILayout.FloatField("Distance",myDistance);

 if(GUI.changed)
 {
 }

}

@MenuItem ("Azylum Tool/Add GeoPainter To Selection") static function CreateMenu () { // Get existing open window or if none, make a new one: if(!GameObject.Find("GeoPainterSys")) { go = new GameObject ("GeoPainterSys"); go.AddComponent("GeoPainter"); Selection.activeGameObject = go; } }

}

GeoPainter.js

class GeoPainter extends MonoBehaviour
{
    var myGroups : Array = new Array();
    var nbrGroupsCreated:int;
}

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

3 Replies

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

Answer by Antony-Blackett · May 10, 2011 at 09:12 PM

Creating a new Array each time is not your problem. The Array will be created when the object is first instantiated. This is before deserialization. Unity uses serialization to save and load data in your scene and project.

The problem you have is that Array class contains 'object' types (not to be confused with UnityEngine.Object) so Unity does not know how to serialize those objects. Unity can only serialize value types like int, string, vector3, as well as many built in UnityEngine classes such as MonoBehaviour, GameObject, etc and custom classes you've written that are marked with the attribute [Serializable]

[Serializable]
public class MySerializableClass
{
}

In your case all you are storing in the array is the GameObject type. Replace the Array in your GeoPainter class with a built in array like this GameObject[].

class GeoPainter extends MonoBehaviour
{
    var myGroups : GameObject[];
    var nbrGroupsCreated:int;
}
Comment
Add comment · 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
0

Answer by Ziboo · May 10, 2011 at 09:25 PM

Thanks for your answer Antony, But I have an error:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
GeoPainterEditor.OnInspectorGUI () (at Assets/_AzylumTool/Editor/GeoPainterEditor.js:12)
UnityEditor.InspectorWindow.OnGUI () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/Inspector/InspectorWindow.cs:364)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)

At the line:

if(target.myGroups.length > 0 && GUILayout.Button ("-")) {

in my OnInspectorGUI

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 Antony-Blackett · May 10, 2011 at 09:47 PM 0
Share

add a test for null first. When build in arrays are length 0 they are often null. I don't know for sure but they might always be null in this case. if(target.myGroups != null && target.myGroups.length > 0 && GUILayout.Button ("-")) {

avatar image Antony-Blackett · May 10, 2011 at 09:51 PM 0
Share

Arrays are partly the reason I only use C# now though. C# collections like List are much easier to work with as you can create a new list List list = new List( myArray ); and then when your array is not null you can add all the elements list.AddRange(myArray);

Then when you're done adding new GameObjects to the list you can convert it back to an array easily. list.ToArray();

C# for the win.

avatar image Eric5h5 · May 10, 2011 at 11:12 PM 0
Share

@Antony Blackett: that has nothing to do with C#. You can do that in JS just as well.

avatar image
0

Answer by Ziboo · May 10, 2011 at 10:27 PM

Thanks for your answers. For C#, I'm from flash, AS3, so javascript is a bit simpler for me.

But it still doesn't fix the issue Antony:

I have something, but my array is still not saving:

GeoPainter.js

class GeoPainter extends MonoBehaviour { private var myGroupsBuiltIn : GameObject[]; var nbrGroupsCreated : int; private var myGroups:Array;

}

GeoPainterEditor.js

@CustomEditor(GeoPainter)

class GeoPainterEditor extends Editor {

...

function OnInspectorGUI () { if(target.myGroups == null) { target.myGroups = new Array(target.myGroupsBuiltIn); target.myGroups.RemoveAt(0); return; }

 ...
 ...
 ...

 target.myGroupsBuiltIn = target.myGroups.ToBuiltin(GameObject);

}

}

I have to make a "target.myGroups.RemoveAt(0)" cause it displayed a "none gameObject"... Maybe because when you create it "private var myGroupsBuiltIn : GameObject[]" it creates it with 1 gameObject inside ?

I'm really kind of lost, and stuck...

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Instantiate into array : Out of range? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can i use objects from a builtin Array to populate a Js Array 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