Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 unity_W8RqHkclGRdmbw · Mar 28, 2018 at 06:48 PM · editor-scriptingsaving

Permanent Variable Change in Editor

If you have a public variable you can change it in the Editor and the change will get saved, maybe that is what all the Serialization stuff is about. It works fine when you change it by hand but what if I want a script to run in the Editor to change the value?

From what I can tell, the change will get overriden. I attempted to have a second script that changes the value of the first one and it itself had the ExecuteInEditMode attribute. Unfortunately the value would be reset the next time I opened Unity. My second attempt was to create a custom inspector for the variable that needs to be set in the editor. I set the value in OnInspectorGUI on that one. Here are some snippets to show you what I mean:

 public class Pickup : MonoBehaviour {
     public string id = "";
     ...
 }
 
 [CustomEditor(typeof(Pickup))]
 public class Identifier : Editor {
     public override void OnInspectorGUI() {
         base.OnInspectorGUI();
         Pickup pickup = (Pickup)target;
         if (pickup.id == "") {
             pickup.id = Time.realtimeSinceStartup.ToString();
         } 
       ...
     }
 }

The Identifier will set the pickup id but the pickup won't remember after closing Unity. It works fine when I set it manually. What can I do to get it to save the value after the editor script changed it?

Alternatively, the bigger goal here is to have a unique identifier for each pickup, whenever I drag a pickup prefab into the scene. This identifier has to be permanent. If you happen to know a different solution for that problem, I will take that as well ;)

Thanks Jona

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
1

Answer by dsabatier · Mar 28, 2018 at 07:03 PM

If goal is persisting data outside of playmode I'd recommend using scriptable objects for this, it will likely resolve your problem. Changes made to scriptable objects with playmode enabled will persist beyond :)

I have seen an implementation where nearly every primitive value in the project was a reference to a scriptable object. For example, a players health would be a scriptable object of type 'IntegerReference'. Then whenever you change that player health object, whether by editor script or in the inspector, that value will remain the same. In one of my projects I had a 'PlayerStats' scriptable object that could hold other scriptable objects for stats, inventory, etc, etc.

You can get started here: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects

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 unity_W8RqHkclGRdmbw · Apr 02, 2018 at 08:10 PM

Learning about scriptable objects was very interesting but then I found that the following script (Identifier.cs) worked too.

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class Identifier : MonoBehaviour {
 
     public string id = "";
 
     private void OnEnable() {
         if(id == "") {
             id = (Time.realtimeSinceStartup + GetInstanceID()).ToString();
         }
     }
 }

Not sure what I did wrong before but it generates a fairly unique identifier that will stay forever. In my Pickup script, I use [RequireComponent(typeof(Identifier))] so it automatically gets an identifier.

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 ClumsyLobster · Apr 02, 2018 at 09:48 PM

https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data

This will show you how to persist data even when closing/opening Unity.

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

87 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 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 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 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 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

OnWillSaveAssets issue 1 Answer

How do you save changes made with custom editor? 3 Answers

Persistence of saving data not working 2 Answers

GameObject Icon not saving 0 Answers

PlayerPrefs.GetString not saving past values 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