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 /
  • Help Room /
This question was closed Jul 03, 2017 at 01:23 PM by Larsupulami for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Larsupulami · May 31, 2017 at 12:05 PM · editorvariableplaymodepersistence

Value set in Editor isnt persistent in Playmode.

I try to change a variable in edit mode with a function via [ContextMenu("SwitchInstructor")] but after I started the play mode the variable is still false.

 public class GameManager : Singleton<GameManager>
 {
     private bool isInstructor = false;
     public bool IsInstructor
     {
         set
         {
             isInstructor = value;
             Debug.Log("IsInstructor " + isInstructor + "  " + GetInstanceID());
         }
         get
         {
             Debug.Log("IsInstructor " + isInstructor + "  " + GetInstanceID());
             return isInstructor;
         }
     }
 
     [ContextMenu("SwitchInstructor")]
     public void SwitchIsInstructor()
     {
         Debug.Log("IsInstructor "+isInstructor + "  " + GetInstanceID()+ "  SwitchIsInstructor ");
         isInstructor = !isInstructor;
         Debug.Log("IsInstructor " + isInstructor + "  " + GetInstanceID()+ "   SwitchIsInstructor");
     }
 }

alt text

I tried to set it with the property and direct. Both methods dosnt work. It doesn't get set at any other place in my code. No custom editor script. No other script with permission to change it. The code for the Singleton class can be found here: http://wiki.unity3d.com/index.php/Singleton

unitydebuglog.png (23.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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by PizzaPie · May 31, 2017 at 01:48 PM

Add [SerializeField] over private bool isInstructor = false;

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 Larsupulami · May 31, 2017 at 02:04 PM 0
Share

thx :) works great but I added: [SerializeField] [HideInInspector] Because it shouldn't be visible in the inspector. Do you have any explanation why it is this way? Fun fact: it doesn't work in the reversed order.

avatar image PizzaPie Larsupulami · May 31, 2017 at 04:03 PM 0
Share

In short because the bool field does not get serialized thus Editor does not know it s value has been changed so it does not save the changes so when you hit play it loads/uses the object without the changes made through the script. Check out the serialization system docs for more info here . There are lot on the subject and it s worth spending time to understand how it works.

avatar image Larsupulami PizzaPie · May 31, 2017 at 05:25 PM 0
Share

If the value is serialized than use serialized if not use new instance. So my script did his thing but didn't save it. $$anonymous$$akes sense. This explanation was awesome thx :)

Follow this Question

Answers Answers and Comments

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

Why is Unity not dislpaying the editor?,Why isn't edit mode apearing? 1 Answer

Why does input only work after a build? 0 Answers

How to enter play mode on two separate Unity instances at the same time? 0 Answers

Bug?: Functionality regarding timeline doesn't work if the timeline is not present in the editor 1 Answer

Custom Editor Inspector member class with inheritance, values resets after starting Play Mode 1 Answer


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