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 Higgcz · Nov 26, 2014 at 12:55 PM · editoreditor-scriptingstaticglobal

How can I preserve static object/data between editor and play?

I made offline Waypoints Network generator, which is able to construct waypoints network in editor so we can move or remove the waypoints during the editing process. However I need some way to reference the waypoints network from different objects in play mode and I really like the singleton approach. My idea is:

I have 3 scripts: WNNetwork, WNNetworkObject and WNNetworkData.

WNNetworkData is simple ScriptableObject, which holds the calculated data.

 [System.Serializable]
 public class WNNetworkData : ScriptableObject {
 
     // Data of waypoints network
     public List<WNWaypoint>     waypoints = new List<WNWaypoint> ();
     public WNKDTree             tree      = null;
 
 }

WNNetworkObject is MonoBehaviour scripts that is attached to GameObject and it is use to update, re-generate or delete the waypoints network.

 public class WNNetworkObject : MonoBehaviour {
 
     #region Public Variables
     
     // Properties of waypoints
     public float size                     = 1f;
     public Color color                     = Color.cyan;
     public Color colorSelected             = Color.white;
     public Color colorLine                 = Color.white;
     public float lineWidth              = 0.5f;
     public WNWaypoint.GizmosType type     = WNWaypoint.GizmosType.CUBE;
     
     // Parameters for network generation
     public float maxClusterRadius            = 2;
     public float neighborsThreshold            = 10f;
     public bool  doNeighborsSimplification     = true;
     
     // Others
     //    public GameObject queryTarget;
     
     #endregion
 
     #region Life-cycle
 
     void Awake () {
         DontDestroyOnLoad (this.gameObject);
     }
 
     void Start () {
         Debug.Log (WNNetwork.data);
     }
 
     #endregion
 ...
 }

This is how it's look in inspector editor:

alt text

The last script is WNNetwork, which is basically a wrapper class holding static reference to WNNetworkData and WNNetworkObject, so I can easily access both.

 public class WNNetwork {
 
     public static WNNetworkObject root;
     public static WNNetworkData data;
 
 ...
 }

I also created an EditorScript, so I can create all objects from Menu, here is the creation part.

 public class CreateWaypointsNetwork {
 
     [MenuItem("GameObject/Create Other/Waypoints Network")]
     public static void Create ()
     {
         WNNetworkData     data = ScriptableObject.CreateInstance <WNNetworkData> ();
         GameObject         go     = new GameObject ("WaypointsNetwork", new System.Type[]{typeof(WNNetworkObject)});
         WNNetworkObject root = (WNNetworkObject) go.GetComponent<WNNetworkObject> ();
 
         WNNetwork.data = data;
         WNNetwork.root = root;
 
         AssetDatabase.CreateAsset (data, "Assets/WaypointsNetworkData");
         AssetDatabase.SaveAssets  ();
 
         EditorUtility.FocusProjectWindow ();
         Selection.activeObject = go;
     }
 
 }

The thing is, when I create the Waypoints Network everything works in editor, every object seems to be successfully created and I can edit the waypoints. But as soon as I hit the play button, the WNNetwork is reset and all static variables are equal to null. The Network itself seems to be preserved, because every waypoint still have reference to all its neighbours, but I cannot access the data.

I know I am doing something terrible wrong, but I'm unable to determine what, I'm still not so familiar with Unity.

Thanks for any help.

screenshot 2014-11-26 13.02.21.png (29.3 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Texaggie96 · Mar 01, 2015 at 05:21 AM

Whenever you press play, Unity throws everything that is not marked for serialization out and reloads all the assemblies. The reason your NetworkData is surviving is because it is a scriptable object and unity knows to serialize those. Your WNNetwork is just a C# class and is not being serialized/stored anywhere (prefab, scriptableobject, etc.) so it is just getting thrown out.

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 BytesCrafter · Mar 11, 2017 at 03:13 PM

In short why is that when I PLAY the game, all public static variable is reset to the default value. But when I used public variable, it does store. Yet I want to make my class like ClassName.Variable.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Event for loading component in the editor 2 Answers

Updating object on inspector value changes in editor 1 Answer

How to get notification of moving a GameObject in the hierachy when editing the scene? 1 Answer

How to check if a key is down in editor script 2 Answers

Are there any autosave scripts out there to protect against losing too much work? 4 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