My script is attached to the parent object.
I want to destroy the child of the parent object.
I wrote this but it obviously doesn’t work…
function Update(){
if(Input.GetKeyDown("1")){
Destroy (child.gameObject);
}
}
Sorry, I am very new to coding.
Mike_L
2
i would do this:
public var destroyChildren = false;
function Update(){
if(Input.GetKeyDown("1")){
destroyChildren = true;
}
}
for the script, and this
function Update () {
if (scriptName.destroyChildren) {
Destroy(gameObject);
}
}
Best thing for me when Im trying to see if pressing a key, or moving the mouse is actually doing anything at all is using the print command. such as
if (Input.GetButton("Jump")){
print("You're pressing the spacebar");
}
just to clarify that the jump button is even triggering anything.
p.s.
go here for your answer http://answers.unity3d.com/questions/4630/destroying-children-sounds-horrible
for ( var t : Transform in transform ) {
Destroy( t.gameObject );
}
Destroys all children of the current object (might destroy the current object but a simple check if t != transform will fix that)
Vicenti:
This is how I implemented the script, however, it is still deleting the parent object that the script is attached to…
if(Input.GetKeyDown("1")){
for ( var t: Transform in transform ) {
if(t!= transform){
Destroy( t.gameObject );
}
}
var LoadBlue : GameObject = Instantiate(Resources.Load("Characters/BallBlue", GameObject));
LoadBlue.transform.parent= transform;
}
if(Input.GetKeyDown("2")){
for ( var t: Transform in transform ) {
if(t!= transform){
Destroy( t.gameObject );
}
}
var LoadYellow : GameObject = Instantiate(Resources.Load("Characters/BallYellow", GameObject));
LoadYellow.transform.parent = transform;
}
I guess you could try
if ( t.IsChildOf( transform ) ) Destroy( t.gameObject );
though I’m not sure why t != transform isn’t working. 
Actually with a small tweak it works perfect, just wanted to say thanks for posting that i was stuck on it as well “not deleting the - children”