Search Unity

Question: Destroy(gameObject.renderer)

Discussion in 'Scripting' started by Morgan, Oct 16, 2006.

  1. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    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

    Code (csharp):
    1.  
    2. function losegame ()
    3. {
    4.     particleEmitter.maxEmission = 3000; //spark burst
    5.    
    6.     Destroy(gameObject.renderer);
    7.  
    8.     rigidbody.velocity = Vector3.zero;
    9.     rigidbody.Sleep();
    10.  
    11.     yield WaitForSeconds (.2);
    12.     particleEmitter.maxEmission = 0; //end burst
    13.  
    14.     yield WaitForSeconds (2);
    15.     particleEmitter.emit = false; //no repeated bursts
    16. }
    17.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    renderer.enabled = false? If destroying the renderer worked before, I think it must have been a bug. :)

    --Eric
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    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.

    Code (csharp):
    1.  
    2. Destroy (GetComponent("MeshRenderer"));
    3.  
     
  4. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Ah - thank you. I vaguely thought it might be some kind of order thing and tried dragging panes up and down the Inspector :eek:

    I must have added things in a different order before.
     
  5. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    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?

    2. 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?

    Code (csharp):
    1.     if (! ball.renderer)
    2.     {
    3.         Destroy(gameObject); //no shadow if ball has exploded
    4.     }
    5.  
    "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.