Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 digitalConundrum · Jul 12, 2011 at 12:30 AM · c#editordisappearoutofboundsunlinked

editor script resetting variables when play mode is triggered

Sorry to bump this, but I've been trying for almost a month with no success and I'd rather bump than start a new topic...I still have no idea why the array is losing all references to the spawned GameObjects when play mode is triggered.

Just as the topic suggests, I have a script running in the editor, which creates a number of gameObjects and then stores references to them in an array within the script.

When I run the script, the custom inspector window I made to view and edit them gets really small and the slider controls and such disappear(which normally occurs when it tries to access an out-of-bounds array index.) This persists until I exit play mode and actually run the script to create the objects again (though if I don't delete the existing ones I get duplicates.)

Is there any way to keep Unity from reloading the script and losing all the information I have stored in memory at the time?

Thanks

EDIT:

This is my entire inspector script, its a very simple prototype of what I eventually want it to look like, but I doubt that this is the issue.

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(HexManager))]
 public class HexInspector : Editor {
 
     int xCoordinate = 0;
     int yCoordinate = 0;
 
         
 
     public override void  OnInspectorGUI()
     {
         HexManager script=(HexManager) target;
         
 
         DrawDefaultInspector();
 
 
       
         EditorGUILayout.Space();
 
          if (script.showEditor)
         {
 
             int minX;
             //Debug.Log(script.xDimension + " " + script.yDimension);
             yCoordinate = EditorGUILayout.IntSlider("Y Coordinate", yCoordinate, 0, script.grid.GetLength(1)-1);
 
 
             minX = yCoordinate;
             //if odd numbered row, is incremented to produce correct hex coordinates
             if (minX % 2 == 1)
             {
                 minX++;
             }
 
             //converting orthagonal grid coordinates into non-orthagonal hex coordinates
             minX = 0 - (minX / 2);
 
 
             xCoordinate = EditorGUILayout.IntSlider("X Coordinate", xCoordinate, minX, script.grid.GetLength(0)-1 + minX);
 
             EditorGUILayout.Space();
             EditorGUILayout.LabelField("Node at","("+xCoordinate + "," + yCoordinate + ")");
             EditorGUILayout.Space();
 
             script.grid[xCoordinate - minX, yCoordinate].passValue = EditorGUILayout.FloatField(
                 "PassValue", script.grid[xCoordinate - minX, yCoordinate].passValue);
 
 
         } 
 
          EditorGUILayout.Space();
          EditorGUILayout.Space();
          EditorGUILayout.Space();
          EditorGUILayout.Space();
          EditorGUILayout.TextArea ("THIS OPERATION CANNOT BE UNDONE, CURRENT GRID WILL BE LOST");
 
         if (GUILayout.Button("Create Grid"))
         {
             
             script.SpawnGrid();
             xCoordinate = 0;
             yCoordinate = 0;
         }
        
     }
 }

And this is the code I use to instantiate and assign the components of the instantiated gameObjects to the array(its very sloppy, I know...I'm not worrying about optimizations until I have it working correctly.)

     while (currentX < xDimension)
 {
         GameObject temp;

         
         if (drawGrid)
         {
             temp = (GameObject)Instantiate(hex, spawnPos, transform.rotation);
             temp.transform.localScale = new Vector3(distanceBetween,distanceBetween,distanceBetween);
             temp.name = xNum.ToString() + "," + yNum.ToString();
             temp.transform.parent = this.transform;
             tempNode = temp.GetComponent<HexNode>();
             tempNode.coordinates = new Vector2(currentX, currentY);
             tempNode.myController = this;
             grid[currentX,currentY]=tempNode;



     
         }
     }


I'm still not sure why the array of hexNodes (grid[,]) is being reset each time I enter and leave play mode...I really have no idea what else to try with this, but it's really becoming a bottleneck with my production right now.

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 Kalan · Oct 03, 2012 at 06:17 PM 0
Share

@$$anonymous$$ Newman I've got similar problem. Did you solve it? If so, how?

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by VoxelBoy · Aug 09, 2011 at 06:10 PM

You need to mark the fields that you want saved/kept-around with [SerializeField]. Make sure the fields are of a Serializable type; I had a List that I needed to be saved and I had to mark the class [System.Serializable].,You need to mark the fields that you want saved or kept-around with [SerializeField]. Make sure the field is a Serializable type; I had a List of a custom class and I had to mark the class [System.Serializable].

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
1

Answer by Armegalo · Apr 21, 2020 at 09:00 PM

As well as serializing everything, make sure you

EditorUtility.SetDirty(myScriptThatWantsSaving)

afterwards

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 nicmarxp · Feb 21 at 07:21 PM 0
Share

This worked great for me, thanks! I had an editorscript that changed a variable in another script using a method in that script. After playing and stopping, it got reset back. So I used exactly this and EditorUtility.SetDirty(this) in the end of the method.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

How do I wait until after AssetDatabase creates an assets before carrying out another function ? 0 Answers

Stop a MonoBehavior from being addable as a script? 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