How do I delete a child of a prefab? NEED EXPERT ADVICE. PLEASE HELP!!!!

I cannot for the life of me access a light inside of a prefab I am instantiating to destroy the light. Yes I want to destroy the light not turn it off. Here is the script along with some of the commands I have tried unsuccessfully. I have tried a every possible combination of the following commands just trying to delete this light I have instantiated as part of a prefab. PLEASE HELP.

start code:

var bullit : Transform;
var spreaderx : float;
var spreadery : float;
var spreaderz : float;
var light2 : Transform;
var lighter : GameObject;
var lighter2 : GameObject;
var lighter3 : GameObject;
function FixedUpdate () {

if(Input.GetKeyDown(“t”))
{

Instantiate (bullit, Vector3(spreaderx, spreadery, spreaderz), Quaternion.identity);

Destroy (light2.transform, 2);
Destroy (light2.gameObject, 2);
Destroy (lighter.gameObject, 2);
Destroy (lighter2.gameObject, 2);
Destroy (lighter3.gameObject, 2);
Destroy(lighter);
Destroy(lighter2);
Destroy(lighter3);
lighting1 = GameObject.Find(“explosion1prefab/light”);
lighting2 = GameObject.Find(“/light”);
lighting3 = GameObject.Find(“light”);
Destroy (lighter.gameObject);
Destroy (lighter2.gameObject);
Destroy (lighter3.gameObject);

Destroy (lighting1.gameObject);
Destroy (lighting2.gameObject);
Destroy (lighting3.gameObject);
}

}

I can destroy normal gameobjects no problem, infact I can even child and unchild from a gameobject and then destroy that gameobject. I can even destroy the light if it is not part of a prefab, but once I create the prefab, I CANNOT DESTROY THE LIGHT.

I can get around this by simply creating 2 instantiated prefabs, 1st being the explosion, the 2nd being the light. Then I can delete the light.

Although I can “MICKEY MOUSE” this by creating 2 prefabs I will not resort to inferior coding just to get the job done. I need to know how to find this light in my prefab and delete it, thank you.

All that matters is the end result! People playing your game wont know how you deleted the light.
With that said, I have no idea how to solve your problem :frowning: I would just go with the work around and move on.

With Light do you mean a Child GameObject with the name “Light” or the Light Component? For the later you should use GetComponent instead of Find and for the first one you should not use GameObject.Find to seach all objects but instead Transform.Find:

ligher.transform.Find("Light")

Just try this below csharp script. Put this script on a grouped gameObject.

I think it may help u.

using UnityEngine;
using System.Collections;

public class deleteObject: MonoBehaviour {

void Start(){
Transform[ ] objectList;
objectList = gameObject.GetComponentsInChildren();
foreach (Transform tempObject in objectList) {
if(tempObject.gameObject.name==“Your_Object_Name”){
Destroy(tempObject.gameObject);
}
}
}
}

thanks guys. i will try this c# script that ram suggested. mark what i mean is its a prefab, and it has a child object named light which is a spotlight that i created. this is all in an attempt to make an explosion more realistic with lighting effects that are deleted less than a second after the explosion goes off to give an ambient lighting effect.

here is how the prefab looks:

  • explosion1prefab
    -embers
    -firecloud
    -light <-------- spotlight i am trying to delete
    -particle1
    -particle2
    -particle3
    -particle4
    -particle5
    -smokey1
    -smokey2
    -sparks1

ok the c# script worked. thanks a lot man. I attached the deleteObject script to the prefab parent and it works.

using UnityEngine;

using System.Collections;

public class deleteObject: MonoBehaviour {

void Start(){

Transform[ ] objectList;

objectList = gameObject.GetComponentsInChildren();

foreach (Transform tempObject in objectList) {

if(tempObject.gameObject.name==“light”) {

Destroy(tempObject.gameObject);

}

}

}

}

Have you seen detonator?

I wouldnt even bother trying to create my own explosions (and dont). Detonator does it so well.

ok now I am back to another basic problem, I cannot put a float delay on the light delete.
Destroy(tempObject.gameObject, 1); this command works
Destroy(tempObject.gameObject, .1); this command does not work
yield WaitForSeconds(2); this command does not work, what the hell?

HOW CAN I PUT A SIMPLE .1 second PAUSE BEFORE MY LIGHT GETS DELETED THANKS…

using UnityEngine;
using System.Collections;

public class deleteObject: MonoBehaviour {

void Start(){
Transform[ ] objectList;
objectList = gameObject.GetComponentsInChildren();
foreach (Transform tempObject in objectList) {
if(tempObject.gameObject.name==“light”) {
Destroy(tempObject.gameObject);
}
}
}
}

thanks yes I have. Those effects look good but I want to learn how to make my own from scratch.

here is the current explosion I am working on:

it needs some work for sure, lighting, some distortion, and
some more decent smoke. the lightsource will help me a lot here…

fair enough then. Your explosion doesnt look too bad.

If I was you, I wouldnt bother deleting the light as opposed to turning it off… any way

Destroy(tempObject.gameObject, .1); this command does not work

needs to be

Destroy(tempObject.gameObject, 0.1f);

Anything that has a decimal place in c# needs to be suffixed with an f

thanks jlcnz.

well slap my ass and call me sally, it worked.

cheers mate.

heres the updated explosion:

No, don’t destroy objects. Just hide it and re-use it when you want to use it again. Otherwise, you’ll get some major CPU overhead from the garbage collector.

That’s false. F means float, which you can use if you have a float variable and want to assign a non-float value to it.

//This will work:
float myfloat = 0;
//This is better because you're being explicit:
float myfloat = 0F;
//This won't work:
float myfloat = 0.0;
//This is required:
float myfloat = 0.0F;

Actually its true. And you just proved it in your own post. Well done /slow clap

float f1 = 0; // fine. no decimal place
float f1 = 0f; // fine. no decimal place
float f1 = 0.0; //fails… has a decimal place… needs the f.
float f1 = 0.0f; //fine. has a decimal place and an f

In fact he is right, the f postfix just marks the value as a float type, leave it away and use a decimal place and you got a double.

0f is in fact a value with an decimal place, a float, even if you dont type out the dot.

float f1 = 0.0 only fails because you cant assign a double to a float value, thats all.

double d1 = 0; // fine
double d2 = 0f; // fine
double d3 = 0.0f; // fine
double d3 = 0.0; // fine
double d4 = 0.0M; // fails, even if its a decimal point
decimal d5 = 0.0M; // fine

No one was trying to assign a float into a double.

Here’s a slow clap for you too captain obvious…

His response was to your statement that every decimal in C# needs to have f appended to it - which is inaccurate. Maybe try to be a bit more respectful?

Guess I should have worded it a bit more obvious, but I assumed I didnt since we were dealing with floats.

Any FLOAT that has a decimal place in c# needs to be suffixed with an f

You are not a very nice person to talk with, try to remove your arrogance and your quick temper.

Warm greetings to you, sir.

We havn’t talked… call me :wink:

I’m not all that bad, but if you catch me in a grump watch out… lol…