object dont destroy, error script

using UnityEngine; using System.Collections;

public class ChangingRoom : MonoBehaviour { private int _charModeIndex = 0;

private CharacterAsset ca;
private string _charModeTag = "x6";
// Use this for initialization
void Start () {
    ca = GameObject.Find("Character Asset Manager").GetComponent<CharacterAsset>();
    InstantiateCharacterModel();
}
void OnGUI(){
ChageCharacterMash();
}

private void ChageCharacterMash(){
if(GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height - 35,120,30),_charModeTag)){
    _charModeIndex++;
    InstantiateCharacterModel();
}

private void InstantiateCharacterModel(){ switch(_charModeIndex){ case 1: _charModeTag = "camaro"; break; default: _charModeIndex = 0; _charModeTag = "x6"; break; } } if(transform.childCount > 0) for(int cnt = 0; cnt < transform.childCount ; cnt++) Destroy(transform.GetChild(cnt)); GameObject model = Instantiate(ca.characterMesh[_charModeIndex],transform.position,Quaternion.identity) as GameObject;

    model.transform.parent = transform;
    model.transform.rotation = transform.rotation;
}

}

What could be wrong here because I have this error: "Can't destroy Transform component. If you want to destroy the game object please call 'Destroy' on the game object instead. Destroying the transform component is not allowed."

Try reformatting your code. This is not so easy to read. Code should be indented with 4 spaces. Or select your code and press the 0101010 button.

2 Answers

2

It says exactly what is wrong. You are trying to destroy a transform that is attached to a gameObject:

Destroy(transform.GetChild(cnt);

If you want to destroy the children of an object, you need to get the gameObject of the transform:

Destroy(transform.GetChild(cnt).gameObject);

thanks it works :):):):)

@michu86: Don't post comments as an answer in this forum, but click the add comment button underneath my answer. This will keep the forum clean.