Changes made from scripts while in edit mode are lost when entering play

Hi!
I am creating levels for my game and I wanted to automate some processes , for instance placing planets in the scene and giving them some initial values, all this from edit mode. I manage to do so but once I hit the play button all of the set values go null.

I have a script : PlanetsSpawner.cs

It has its own custom editor : PlanetsSpawnerCustomEditor.cs
In this custom editor script I create a simple button and when pressed, we instantiate a GameObject named Planet in to the scene and give it a component named Faction.cs wich It looks like this :

 public class Faction : MonoBehaviour
    {
        // Reference to a ScriptableObject that contains information about the faction.
        FactionSO _myFactionSO;
        public FactionSO MyFactionSO { get => _myFactionSO; set => _myFactionSO= value; }
     
    }

And the code that instantiates Planet, gives it a component Faction.cs and assigns some initial value to it :

public class PlanetsSpawnerCustomEditor: Editor
{
    public override void OnInspectorGUI()
    {
         ...
        foreach(Faction faction in ListOfFactions)
        {
            if (GUILayout.Button("Spawn Planet"))
            {
                futurePlanet = new GameObject("Planet");
                var futurePlanetFaction= futurePlanet.AddComponent<Faction>();
                futurePlanetFaction.MyFactionSO = faction.MyFactionSO;
                var instantiatedPlanet= Instantiate(futurePlanetFaction, Vector3.zero, Quaternion.identity, null)
            }
        }
    }
}

I simplified the script so it’s easier to understand what is going on.

foreach(Faction faction in ListOfFactions)

ListOfFactions it’s a list contained in PlanetsSpawner.cs but we access to it from it’s custom editor script.
When I test this, it behaves as I expect it to do :
8241945--1077840--upload_2022-6-29_21-15-6.png
It does assign in this case the BlueFaction (FactionSO) to the planet but, here is the thing, If I hit play this goes null. I don’t understand why does this happen, I expected it to be similar to assigning the value from inspector using [SerializeField] attribute but it was clearly a wrong hipotesis.
How should I aproach this problem?
Thank you!

I didn’t see anything in the code you showed that would cause this, so I don’t think there’s really anything anyone can do to pinpoint your problem without showing more.

Just for a sanity check though, if you’re on Visual Studio and probably many other IDEs, you can right click MyFactionSO in your Faction class and choose Find All References, and see what code actually affects that variable. My guess is that you might have something set your Faction variable to null on Start() by accident.

This all seems very sus… ^ ^ ^ ^

What is planet and what is instantiatedPlanet and what isplanetInstance and are you sure you are even on the correct planet?

At a bare minimum, give them better names to exhibit their semantic relevance to the code at hand, such as “previousPlanet” or “otherPlanet” or “currentPlanet” or “futurePlanet”

You are right, I got the names a bit mixed when trying to simplify it, I edited them now using one of your suggestions.
Now they should be self explanatory.
And yes, I am sure I am on the correct planet

It’s a nice guess but nope, just one reference. I don’t really know what else could be relevant to show…