- Home /
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?
I'm facing similar problem as well. Is there a solution to this problem?
If you assign it to the prefab be sure you call the SetDirty method.
https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html
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);
}
}
}
}
}
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.
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:
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);
}
}
}
}
}
Could you put this into a separate question? The topic doesn't quite match.
I have created separate question : http://answers.unity3d.com/questions/1362942/scene-gameobject-public-variables-assigned-by-scri.html