ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED]
I'm trying to make an automated system, where you type how many enemies you want in the scenes. And then it automatically saves every vector position of all the instantiate Enemies.
It has been working, and the saving and instantiate in EditMode has been working until I decided to try to transform the enemy GameObject as a child to another GameObject in the scene. Now it only will instantiate the enemy object if the game is running, which defeats the whole purpose.
Edit* This does work* [ExecuteInEditMode] public class FrameScriptModeEditor : MonoBehaviour {
public GameObject myEnemy;
public int amountOfMyEnemy;
public string nameMyEnemy;
public Transform currentFramePlaceholder;
// Start is called before the first frame update
void Start()
{
currentFramePlaceholder = GameObject.Find("Placeholder").transform;
}
// Update is called once per frame
void Update()
{
if (amountOfMyEnemy > 0)
{
currentFramePlaceholder = GameObject.Find("Placeholder").transform;
for(int i = 0; i < amountOfMyEnemy; i++)
{
GameObject newEnemy = Instantiate(myEnemy, new Vector3(0, 0, 0), Quaternion.identity);
newEnemy.name = nameMyEnemy + i;
newEnemy.transform.parent = currentFramePlaceholder.transform;
if(amountOfMyEnemy == 0)
{
i = 0;
}
}
}
}
}
But if I do it like this I can instantiate the object while the game is not running, aka in Edit mode. But then I get this Error "Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption"
[ExecuteInEditMode]
public class FrameScriptModeEditor : MonoBehaviour
{
public GameObject myEnemy;
public int amountOfMyEnemy;
public string nameMyEnemy;
public Transform currentFramePlaceholder;
// Start is called before the first frame update
void Start()
{
currentFramePlaceholder = GameObject.Find("Placeholder").transform;
}
// Update is called once per frame
void Update()
{
GameObject newEnemy = myEnemy;
if (amountOfMyEnemy > 0)
{
currentFramePlaceholder = GameObject.Find("Placeholder").transform;
for(int i = 0; i < amountOfMyEnemy; i++)
{
Instantiate(newEnemy, new Vector3(0, 0, 0), Quaternion.identity);
newEnemy.name = nameMyEnemy + i;
newEnemy.transform.parent = currentFramePlaceholder.transform;
if(amountOfMyEnemy == 0)
{
i = 0;
}
}
}
}
}
My question is how can I instantiate a prefab as a child to another gameObject while in edit mode?
Answer by Banditmayonnaise · Feb 26, 2021 at 11:12 AM
I just discovered, that it actually did work... I just missed that there was 2 Gameobject that was named Placeholder, so it just instantiates in the wrong placeholder. Big Yikes...