- Home /
Question by
SatyrIX · Jun 02, 2014 at 07:19 PM ·
gameobjectinstantiatetransformprefabdestroy
Can't remove instantiated prefab
I am trying to create buttons that will instantiate and remove two sprite prefabs using UnityEngine; using System.Collections;
public class PopulateLevel : MonoBehaviour {
public GameObject greenOrcMelee;
public GameObject redOrcMelee;
private Vector3 dispatchVector;
private Object[] greenArmy;
private Object[] redArmy;
// Use this for initialization
void Start () {
greenArmy = new Object[3];
redArmy = new Object[3];
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
GUI.Box (new Rect(5, 32, 200, 200), "Green Orcs");
GUI.Box (new Rect(1700,32, 200, 200), "Red Orcs");
int side;//1=green, 2=red
int army;
if(GUI.Button (new Rect(10, 70, 100, 50), "Army 1")){
side = 1;
army = 1;
dispatchArmy(side, army);
}
if(GUI.Button (new Rect(10, 118, 100, 50), "Remove")){
side = 1;
army = 1;
removeArmy (side, army);
}
if(GUI.Button (new Rect(1710, 70, 100, 50), "Army 1")){
side = 2;
army = 1;
dispatchArmy (side, army);
}
if(GUI.Button (new Rect(1710, 118, 100, 50), "Remove")){
side = 2;
army = 1;
removeArmy (side, army);
}
}
void dispatchArmy(int side, int army){
if(side == 1){
dispatchVector = new Vector3(2, 5, -5);
if(army == 1){
greenArmy[0] = Instantiate (greenOrcMelee, dispatchVector, Quaternion.identity);
greenArmy[1] = Instantiate (greenOrcMelee, new Vector3(dispatchVector.x-1, dispatchVector.y+1, dispatchVector.z), Quaternion.identity);
greenArmy[2] = Instantiate (greenOrcMelee, new Vector3(dispatchVector.x-1, dispatchVector.y-1, dispatchVector.z), Quaternion.identity);
}
}else if(side == 2){
dispatchVector = new Vector3(18, 5, -5);
if(army == 1){
redArmy[0] = Instantiate (redOrcMelee, dispatchVector, Quaternion.identity);
redArmy[1] = Instantiate (redOrcMelee, new Vector3(dispatchVector.x+1, dispatchVector.y+1, dispatchVector.z), Quaternion.identity);
redArmy[2] = Instantiate (redOrcMelee, new Vector3(dispatchVector.x+1, dispatchVector.y-1, dispatchVector.z), Quaternion.identity);
}
}
}
void removeArmy(int side, int army){
if(side == 1){
if(army == 1){
for(int i = 2; i > -1; i--){
GameObject.Destroy (greenArmy[i]);
}//end army1 for
}
}else if(side == 2){
if(army == 1){
for(int i = 2; i > -1; i--){
GameObject.Destroy (redArmy[i]);
}//end army1 for
}
}
}//end removeArmy
}
Cloning and removing the greenOrcMelee's works just fine, as does cloning the redOrcMelee's. However, when I click the button to remove the redOrcMelee's, I get the error "Can't destroy Transform component of 'redOrcFigther(Clone)'. If you want to destroy the game object, please call 'Destroy' on the game object instead. Destroying the transform component is not allowed." I'm wondering why this happening because the code to remove them is pretty much identical to the greenOrcMelee's.
Comment