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 :

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!