- Home /
Scene gameobject public variables assigned by script in editor doesnot persist - but manually entered values in editor UI persists
Hi Guys, Im facing problem with uniquely taging unity gameobjects. I want to assign unique tag to gameobjects that persists. 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.
[MenuItem("PMS/Tag Buttons")]
public static void TagButtons()
{
TagScenesGameObjects();
}
public static void TagScenesGameObjects()
{
string[] allScenes = GetAllScenes();
foreach (string scene in allScenes)
{
EditorSceneManager.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 Adam-Mechtley · Jun 12, 2017 at 06:43 AM
Whenever you change something at edit time, if you do not use SerializedProperty to do it, you need to register the changes with the Undo system in order for the Editor to correctly pick it up as dirtied. Before you change the tag on the GameObjects you should do something like Undo.RecordObjects(sceneGOs.Cast<UnityEngine.Object>().ToArray(), "Change Tags")
(assuming you are using Linq. Obviously do it differently however you prefer).
Your answer
![](https://koobas.hobune.stream/wayback/20220612123741im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
make game objects not move in scene view 2 Answers
Creating AssetBundles from Editor Scripts 1 Answer
Generalizing My GameObjects And Scripts in Unity 0 Answers
Regarding setActive 4 Answers
Draw Decal on Floor using a Path 0 Answers