Question: Destroy(gameObject.renderer)

In the past, I have been able to make an object invisible, yet still have particles coming from a mesh emitter, simply via Destroy(gameObject.renderer). The mesh vanishes, but particles continue.

But this time it’s not working: if I destroy the renderer (see below), the particles ALSO vanish. Why might that be?

The only difference I can see is that this time I’m using en ellipsoid particle emitter instead of a mesh emitter. But why would that cause this effect?

Is there a way I can make the mesh invisible and still allow the particles to remain?

TIA

function losegame ()
{
	particleEmitter.maxEmission = 3000; //spark burst
	
	Destroy(gameObject.renderer);

	rigidbody.velocity = Vector3.zero;
	rigidbody.Sleep();

	yield WaitForSeconds (.2);
	particleEmitter.maxEmission = 0; //end burst

	yield WaitForSeconds (2);
	particleEmitter.emit = false; //no repeated bursts
}

renderer.enabled = false? If destroying the renderer worked before, I think it must have been a bug. :slight_smile:

–Eric

You have two renderer’s on the game object. Both MeshRenderer and ParticleRenderer inherit from Renderer.

The call to renderer returns you the first Renderer in the game object. So if you have two renderers in the game object the one that shows up first in the inspector will be removed.

So you could be more explicit about what you want to remove, or re-add components in such a way that the mesh renderer comes first.

Destroy (GetComponent("MeshRenderer"));

Ah - thank you. I vaguely thought it might be some kind of order thing and tried dragging panes up and down the Inspector :o

I must have added things in a different order before.

That destroy code does make the mesh vanish, but causes two other issues:

  1. A static var gets cleared too–why would that be?

The script has static var playing = true; at the top… but now it becomes false (or maybe null). If I comment out the destroy line, all is well again. There’s no other code changing that var, so why might it be affected by the mesh renderer?

  1. Another object (a shadow) needs to detect whether the mesh is rendering or not. That worked before, but not with the new destroy syntax. Is there another way I should achieve the result below?
	if (! ball.renderer)
	{
		Destroy(gameObject); //no shadow if ball has exploded
	}

“ball” is the object with the mesh I am hiding, and the above script goes on the shadow object, to make it destroy itself when no longer needed.

Thanks again.