this is the script currently. I want the objects to be grouped per modelName, and all together under a single parent. When the script is executed, the old objects must be cleared:
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using Unity.EditorCoroutines.Editor;
[ExecuteInEditMode]
public class CSVScenePopulator : MonoBehaviour
{
public string csvFileName = “05_ClonedAssets_export_20250516_1119.csv”;
public GameObject parentAsset; // Single asset containing all children prefabs
public float positionScale = 1;
private Dictionary<string, GameObject> prefabCache = new Dictionary<string, GameObject>();
Debug.LogError("Parent asset is not assigned.");
string path = Path.Combine(Application.streamingAssetsPath, csvFileName);
EditorCoroutineUtility.StartCoroutineOwnerless(PopulateSceneAsync(path));
Debug.LogError("CSV file not found at: " + path);
private System.Collections.IEnumerator PopulateSceneAsync(string path)
string[] lines = File.ReadAllLines(path);
// Cache child prefabs from parent asset
foreach (Transform child in parentAsset.transform)
prefabCache[child.name] = child.gameObject;
// Instantiate prefabs from CSV data
foreach (var line in lines[1..]) // Skip header
var values = line.Split(',');
string modelName = values[2].Trim().Replace(" ", "_");
if (!prefabCache.ContainsKey(modelName))
Debug.LogWarning("Model not found in parent asset: " + modelName);
Vector3 position = new Vector3(
Quaternion rotation = Quaternion.Euler(
Vector3 scale = new Vector3(
GameObject instance = Instantiate(prefabCache[modelName]); // (GameObject)PrefabUtility.InstantiatePrefab(prefabCache[modelName]);
instance.transform.SetPositionAndRotation(position, rotation);
instance.transform.localScale = scale;
yield return null; // Yield to avoid freezing editor
Debug.Log("Scene population complete.");
}