[HELP] Setting the parent of a transform which resides in a prefab is disabled

Hi!

Im trying to instantiate a prefab from an array as a game object as a child of EnemySpawner GameObject.

The weird thing is, the first enemy spawns properly and is properly placed as a child. But the following one spawns not as a child and I get the error “Setting the parent of a transform which resides in a prefab is disabled”

using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour {

	public GameObject[] enemies;
	
	public GameObject enemy;
	
	
	void Start(){
			Spawn();
	}
	
	public void Spawn(){
			Debug.Log ("Spawning new enemy!");
			enemy = Instantiate (enemies[Random.Range(0,4)],transform.position,Quaternion.identity) as GameObject;	
			enemy.transform.parent = transform;

	}
	

}

And here is my enemy script if you need it.

using UnityEngine;
using System.Collections;

public class EnemyBehaviour : MonoBehaviour {

	public EnemySpawner spawnScript;
	public float health;
	public GameObject[] enemy;

	
	void OnTriggerEnter2D(Collider2D collider){
		Projectile bullet = collider.gameObject.GetComponent<Projectile>();
		if(bullet){
			health -= bullet.GetDamage();
			bullet.Hit();
			if (health <= 0){	
				Destroy(gameObject);
				spawnScript.Spawn();
				Debug.Log ("Enemy Destroyed");	
				
			}
		}
	}
}

What am I missing to put the following prefabs into the right parent?

The error is indeed odd- you clearly just Instantiated the object. Perhaps you need to “disconnect” it with PrefabUtility.DisconnectPrefabInstance. But that would be odd too, particularly as that’s editor only stuff.

A possible workaround; use the last listed version of Instantiate, that takes the new object’s parent, as the last parameter:

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);