Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mesh Render Toggle script?

Discussion in 'Scripting' started by Calledtogaming, Mar 8, 2010.

  1. Calledtogaming

    Calledtogaming

    Joined:
    Mar 2, 2010
    Posts:
    16
    Is there a script that will allow me to render meshes off and on when something happens, including turning on meshes in game that are turned off during editing (for cpu sake)
     
  2. CoatlGames

    CoatlGames

    Joined:
    Apr 25, 2008
    Posts:
    773
    it depends on your specific use, you may disable the mesh renderer of that gameobject, if you still want that gameobject to be active or if its there some script you wish to remain active, or you can disable the entire gameobject where the mesh is attached
     
  3. Calledtogaming

    Calledtogaming

    Joined:
    Mar 2, 2010
    Posts:
    16
    Well, basically, I'm going to be building a lot on one scene until i can figure out the spawning between scenes issue I'm having. So i'm wondering if i can disable the mesh (just the mesh) to save on video memory while i'm building on the scene and then when the game is running have a script that reactivates the mesh.

    For example, If I make a door that leads to a cave. Before I go in that door, I'd have the cave unrendered, and when I go in the door, it would re enable the mesh render and possibly disable the above ground mesh render until i left the cave again. All to save on cup usage so the game doesn't crash during creation or play.
     
  4. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Unity does what you're asking for automatically to a certain degree. A mesh that isn't in the view frustum will not be rendered. This would all hinge on your cave model not being one contiguous mesh, but broken up into appropriate pieces. As well as tweaking the far plane of the camera.

    You could also turn objects on or off, based upon triggers as you open the door and progress through the cave. There's much talk about such techniques for performance tweaking in the following video.

    http://unity3d.com/support/resources/unite-presentations/performance-optimization

    Perhaps the best thing to save you headaches would be to solve the 'spawning between levels' problem you say you have. Have you started a thread on it?
     
  5. Calledtogaming

    Calledtogaming

    Joined:
    Mar 2, 2010
    Posts:
    16
    I have indeed started a thread on it, unfortunately with less than desirable results. I understand the concepts and what's needed in the scenes but it's the application of the scripting that boggles me to a great degree.

    Basically, I need to figure out how to tell the system. "When I load Scene 2, from scene 1, spawn me here. When I load Scene 1 to scene 2, spawn me here, But when i load Scene 1 from scene 3, spawn me here instead". something along those lines.

    It's like when you come out of a dungeon in oblivion, you spawn right outside the door, everything's as you left it outside and inside.

    (i don't know if i'm making sense anymore, But that's essentially my problem. I can't seem to successfully set spawn points between scenes while maintaining all the information that has changed during a scenes activation.)
     
  6. xortrox

    xortrox

    Joined:
    Feb 13, 2011
    Posts:
    22
    var myMeshRenderer:MeshRenderer;
    //drag the objects MeshRenderer over this variable


    var RenderMesh:boolean = true;
    //this will give you a toggle box on the object
    //containing the mesh



    function Update()
    {

    if(RenderMesh)
    {
    myMeshRenderer.enabled = true;
    }


    if(!RenderMesh)
    {
    myMeshRenderer.enabled = false;
    }

    }
    // also attacheed this
     

    Attached Files:

  7. veganophob

    veganophob

    Joined:
    Apr 15, 2012
    Posts:
    2
    hi there,

    i am having problems with your script.
    i have a prefab with many objects in it and 1 script for all.
    i would like to disable the meshrenderer for one of the objects (Transform) by script, but it is not working
    "Meshrenderer is not a member of UnityEngine.Transform" i also tried test.myMeshrenderer but it didnt change anything.

    what can i do?
     
  8. Kastar-Troy

    Kastar-Troy

    Joined:
    Jul 18, 2012
    Posts:
    29
    Just wrote this little bit of code if anyone stumbles upon this:

    Re-usable method you can use anywhere, disables all renderers.