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
1
Question by mgc90403 · Jun 28, 2015 at 06:32 PM · editoreditor-scriptingpublic variable

public variables assigned by script in editor vanish at runtime - but manually entered values persist

I'm using an editor script to assign values to public variables on a game object. When I press "Play", those values vanish, in game - - they're also gone when the game stops running and I'm back in editor mode.

If I enter those exact same values by hand rather than script, then they persist when the game runs.

How can I get unity to save those values assigned by script?

Comment
Add comment · Show 3
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 Saud-Ali · Jun 07, 2017 at 11:23 AM 0
Share

I'm facing similar problem as well. Is there a solution to this problem?

avatar image Masterio · Jun 07, 2017 at 11:32 AM 0
Share

If you assign it to the prefab be sure you call the SetDirty method.

https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html

avatar image Saud-Ali Masterio · Jun 08, 2017 at 06:54 AM 0
Share

Im facing problem with uniquely taging unity gameobjects. I want to assign unique tag to gameobjects. But every time I run this method a new tag is assigned to gameobjects in scene. Is there something I need to do to fix this issue ?

If I execute this method (TagButtons : mentioned below) many times; tags are assigned and are shown fine in unity editor but tags older values are read in script. If I edit values in editor they are stored correctly in unity. But somehow values are not storing correctly when editing values through script.

  [$$anonymous$$enuItem("P$$anonymous$$S/Tag Buttons")]
  public static void TagButtons()
  {
        TagScenesGameObjects();
  }

  public static void TagScenesGameObjects()
  {
      string[] allScenes = GetAllScenes();
      foreach (string scene in allScenes)
      {
          EditorScene$$anonymous$$anager.OpenScene(scene);
          GameObject[] sceneGOs = GetAllObjectsInScene();
          foreach (GameObject gameObject in sceneGOs)
          {
              Button[] btnList = gameObject.GetComponents<Button>();
              foreach (Button btn in btnList)
              {
                  ButtonController buttonController = btn.gameObject.GetComponent<ButtonController>();
                  // First time add a valid new tag
                  if (buttonController == null)
                  {
                      buttonController = btn.gameObject.AddComponent<ButtonController>();
                      buttonController.SetTags(tag);
                  }
                  else
                  {
                      string tag = buttonController.GetTags();
                      string validTag = ValidTag(tag);
                      buttonController.SetTags(validTag);
                      EditorUtility.SetDirty (btn.gameObject);
                  }
              } 
          }
      }
  }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by daxiongmao · Jun 07, 2017 at 05:10 PM

Use the editorutility.setdirty. This will mark the values as changed. Then when you run they will not get reverted.

https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html

I just noticed the say not to use this in scenes anymore. But the page shows the updated way.

You can still use this though. And is the way you do it for prefabs.

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 hexagonius · Jun 07, 2017 at 02:56 PM

If you look at the two custom editor scripts here, you should be able to see what the difference is. If you've written your logic differently, you should consider using one of the described techniques:

https://docs.unity3d.com/ScriptReference/Editor.html

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 Saud-Ali · Jun 08, 2017 at 06:53 AM 0
Share

Hi Guys, Im facing problem with uniquely taging unity gameobjects. I want to assign unique tag to gameobjects. But every time I run this method a new tag is assigned to gameobjects in scene. Is there something I need to do to fix this issue ?

If I execute this method (TagButtons : mentioned below) many times; tags are assigned and are shown fine in unity editor but tags older values are read in script. If I edit values in editor they are stored correctly in unity. But somehow values are not storing correctly when editing values through script.

     [$$anonymous$$enuItem("P$$anonymous$$S/Tag Buttons")]
     public static void TagButtons()
     {
           TagScenesGameObjects();
     }

     public static void TagScenesGameObjects()
     {
         string[] allScenes = GetAllScenes();
         foreach (string scene in allScenes)
         {
             EditorScene$$anonymous$$anager.OpenScene(scene);
             GameObject[] sceneGOs = GetAllObjectsInScene();
             foreach (GameObject gameObject in sceneGOs)
             {
                 Button[] btnList = gameObject.GetComponents<Button>();
                 foreach (Button btn in btnList)
                 {
                     ButtonController buttonController = btn.gameObject.GetComponent<ButtonController>();
                     // First time add a valid new tag
                     if (buttonController == null)
                     {
                         buttonController = btn.gameObject.AddComponent<ButtonController>();
                         buttonController.SetTags(tag);
                     }
                     else
                     {
                         string tag = buttonController.GetTags();
                         string validTag = ValidTag(tag);
                         buttonController.SetTags(validTag);
                         EditorUtility.SetDirty (btn.gameObject);
                     }
                 } 
             }
         }
     }
avatar image hexagonius Saud-Ali · Jun 08, 2017 at 07:05 AM 0
Share

Could you put this into a separate question? The topic doesn't quite match.

avatar image Saud-Ali hexagonius · Jun 08, 2017 at 07:19 AM 0
Share

I have created separate question : http://answers.unity3d.com/questions/1362942/scene-gameobject-public-variables-assigned-by-scri.html

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

25 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

Related Questions

RequireComponent with custom public vars? 1 Answer

Instance of [Editor] couldn't be created because there is no script with that name. 1 Answer

Undo.RecordObject isn't working. 7 Answers

Can you run custom code when changing scene perspective? 1 Answer

Event for loading component in the editor 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