The question is answered, right answer was accepted
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");
}
}
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
Answer by PizzaPie · May 31, 2017 at 01:48 PM
Add [SerializeField] over private bool isInstructor = false;
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.
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.
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
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