- Home /
Weird behaviour of an editor extension that sets variables on other scripts.
I have an editor extension that generates unique ids for objects with selected tags, I needed to make it since unity doesn't have a reliable id system to use in a save/load funcionality. the idea is that when i'm done with making a level I run the ID dispenser and everything that needs an id has it.
The problem is for all gameobjects with a few tags it works as intended, but for a few others it doesn't - it sets the id when I run the dispenser, but then when I hit play ids are set to = "". This is an example from the editor extension:
string prefix_pickup = "P-";
void DispenseIDToPickups()
{
if(prefix_pickup == null || prefix_pickup == "")
{
return;
}
GameObject[] pickups = GameObject.FindGameObjectsWithTag("Pickup");
string id = "";
for (int i = 0; i < pickups.Length; i++)
{
id = prefix_pickup + i.ToString("D6");
pickups[i].GetComponent<PickupFunctionPasser>().SetUniqueID(id);
}
}
PickupFunctionPasser is just a class from which all pickups inherit in my project, it has a bunch of virtual voids, so you can run override voids on those pickups through those virtual voids.
Now this is what sits on an example pickup:
[HideInInspector]
public string id;
public override void SetUniqueID(string passed_id)
{
id = passed_id;
Debug.Log("id for " + gameObject.name + " is " + id);
}
public override string GetUniqueID()
{
return id;
}
That debug.log actually returns the id that is supposed to be there, so the system works, untill i press play - then the id = ""; How do I make it stay there? And why does it stay on some other objects even though they use a setup like that?
EDIT: So I've tried using the Undo.RecordObjects and Undo.RecordObject this way:
void DispenseIDToPickups()
{
if(prefix_pickup == null || prefix_pickup == "")
{
return;
}
GameObject[] pickups = GameObject.FindGameObjectsWithTag("Pickup");
Undo.RecordObjects(pickups, "assigning ids");
string id = "";
for (int i = 0; i < pickups.Length; i++)
{
id = prefix_pickup + i.ToString("D6");
pickups[i].GetComponent<PickupFunctionPasser>().SetUniqueID(id);
}
}
And this:
void DispenseIDToPickups()
{
if(prefix_pickup == null || prefix_pickup == "")
{
return;
}
GameObject[] pickups = GameObject.FindGameObjectsWithTag("Pickup");
string id = "";
for (int i = 0; i < pickups.Length; i++)
{
id = prefix_pickup + i.ToString("D6");
Undo.RecordObject(pickups[i], "Assigned id");
pickups[i].GetComponent<PickupFunctionPasser>().SetUniqueID(id);
}
}
And the id is still set to ="" when pressing play.
By the looks of it, im guessing its saving/serialization problem. So if you are doing these to scene objects you have to save the scene in order to save the changes. I'm not sure but you could probably use these in your script to fix the random saving:
You should probably only run those from custom editor, hope it helps.
Answer by FortisVenaliter · Aug 24, 2017 at 04:30 PM
Check out this article. Looks like Unity may have changed the way you mark changes since 5.3. Instead of MarkDirty, you need to use the Undo system.