- Home /
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.
Answer by Marnix · Apr 14, 2011 at 11:30 AM
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);
Your answer
Follow this Question
Related Questions
Replacing object: first object not completely destroyed? 0 Answers
Object reference not set to an instance of an object (with a RaycastHit) 3 Answers
any tips for creating a scrollable character select screen 1 Answer
How to fix? Character grabbing system 1 Answer
"Destroy assets is not permitted to avoid data loss" 2 Answers