How do i go about generating stuff in editor and keeping it in play mode

So, i’m generating the track using segments and attaching them to the end of the track. I created a custom editor script to add “generate” button to the inspector of my track object. In editor it works fine, but as soon as i hit play, my list of segments is empty, while scene still have those segment objects as children of track.
I tried to assign some assets to segments in the editor and it keeps them. So i guess i’m doing something wrong with serialization considering unity only serializes values if they are set in inspector.

List is defined like that:

[SerializeField]
public List<GameObject> segments; 

And i add segments with that:

void addRandomSegmentsToTrack(int segmentsToAdd)
{
    initializeIfNoSegmentsPresent();
    foreach (var segment in Range(0, segmentsToAdd))
    {     
        segments.Add(Instantiate(segmentPrefab, trackEndPoint, trackEndRotation));
        getSegmentScript(segments.Last()).constructSegment();
        segments.Last().transform.parent = this.transform;
        updateTrackEndPointAndRotation();
    }             
}

    void initializeIfNoSegmentsPresent()
    {        
        if(!segments.Any())
        {            
            trackEndPoint = transform.position;
            trackEndRotation = Quaternion.identity;
        }
    }

So i really puzzled how can i do something in editor and keep this changes in play mode?

I also have problem with unity events calling object methods in the editor. Maybe i’m making architectural mistakes by decentralizing everything and just passing references to scene components in inspector?

Long story short: just set the data dirty after the changes or record object before making the changes. And to be sure it’s saved save the scene automatically (optional)…

Preferred way (to support also undo)

Undo.RecordObject(segments, "My undo add segment"); //this goes before making the change
optional: EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());

or like this (this will not record the change)

EditorUtility.SetDirty(segments); //this goes after the changes
optional: EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());

or set the whole scene dirty (this will not record the changes)

EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); //this goes after the changes
optional: EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());

Notice! Save the scene only once when you have iterated to whole list…

hope you get it working!

Hi @mixer468 , I didn’t understand why you want to do something in editor first then go to the runtime???
and I also didn’t understand your problem with unity events… can you explain more please???
.
BTW you can instantiate game objects at AWAKE Or START Method and put them as a child of a parent with a Horizontal, Vertical Or even Grid Layout group as you need them. Also there is the trick of pooling system to make sure it’s not an expensive call on startup methods and don’t give you any Calculating time, Anyway I hope I got your problem correctly and of course if you’d explain it better, I can help better I believe. good luck, cheers.