How to remove components from objects such as Ellipsoid Particle Emitters

We have a space game where ships fire photon objects that are cloned objects of a prefab of ours. You can see here what I had been doing after a “Photon” projectile is fired. It should disappear after 2.5 seconds, but because it cannot remove the EllipsoidParticleEmitter or the WorldParticleCollider, the object just hangs out in space and does not get destroyed.

“Can’t remove component.”
“Can’t remove Transform because EllipsoidParticleEmitter, WorldParticleCollider depends on it”

From reading some other posts, I gather that Ellipsoid Particle Emitters, among other things, are not accessible in scripts. Our game is in space where we fire a photon projectile that has an Ellipsoid Particle Emitter and a World Particle Collider among other things. Therefore, I gather that trying to remove such a component from an object is not really possible.

if(clone.name == "Laser(Clone)")
{
	Destroy(clone.GetComponent(LineRenderer), 2.5);
	Destroy(clone.GetComponent(TrailRenderer), 2.5);
}

if(clone.name == "Photon(Clone)")
{
	Destroy(clone.GetComponent(EllipsoidParticleEmitter), 2.5);
	Destroy(clone.GetComponent(ParticleAnimator), 2.5);
	Destroy(clone.GetComponent(ParticleRenderer), 2.5);
	Destroy(clone.GetComponent(WorldParticleCollider), 2.5);
}
		
Destroy(clone.GetComponent(Collider), 2.5);
Destroy(clone.GetComponent(Rigidbody), 2.5);
Destroy(clone, 2.5);

How would one go about removing this “Photon” component altogether after 2.5 seconds without simply not having an EllipsoidParticleEmitter or a WorldParticleCollider (which is not the best option). It is recommended that “DestroyImmediate” is not used, but would that be a recommended / possible option if that works? I feel like there should be a way to remove the object…

Hey, actually I figured it out. I had tried the following:

if(clone.name == "Photon(Clone)")
{
	Destroy(clone.GetComponent("Ellipsoid Particle Emitter"), 2.5);
	Destroy(clone.GetComponent(ParticleAnimator), 2.5);
	Destroy(clone.GetComponent(ParticleRenderer), 2.5);
	Destroy(clone.GetComponent("World Particle Collider"), 2.5);
}

This had not worked, but then I tried removing the spaces and it worked.

if(clone.name == "Photon(Clone)")
{
	Destroy(clone.GetComponent("EllipsoidParticleEmitter"), 2.5);
	Destroy(clone.GetComponent(ParticleAnimator), 2.5);
	Destroy(clone.GetComponent(ParticleRenderer), 2.5);
	Destroy(clone.GetComponent("WorldParticleCollider"), 2.5);
}

I’m about to do something very similar. But can’t see where the spaces are that you’ve removed that made it work?

And if you wanna do me a huge favor, can you explain why “Photon(Clone)” is the way it is? Is the string Photon(Clone_is_a_variable) so it might be something like Photon3?

The components don’t work with the spaces because the components don’t really have spaces in the real name. The inspector adds the spaces so that they look friendly. Something like MeshFilter is actually a class name in the API, and class names can’t have spaces. This tends to be hard on the eyes, so the inspector kindly adds spaces for the sake of our eyes.

The reason that he has “(Clone)” after it is because that’s what the engine adds when you create an instance of something from a prefab. It actually has nothing to do with variables or function parameters, even though it looks like it. For instance (no pun intended), if I have a prefab that I’ve named “Laser” and use Instantiate() to make an instance of it, it’s going to be named “Laser(Clone)” in the editor. That’s really meant to distinguish script-instantiated objects from editor-placed objects.

On second thought, yes, the pun IS intended. :slight_smile: