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?