i have the following scenario i have 1 big gamobject/folder with 400 objects inside lets call it ROOM_A a door with a small S-shaped corridor and another bit gameobject/folder with roughly 500 more objects inside lets call it ROOM_B ROOM_A and ROOM_B can never be seen at the same time so i want to turn them off while you are in the other room ! for framerate optimization i was trying to write myself a simple script i could attach to a trigger box where i could choose a gameobject/folder and as long as you are standing inside that box the gameobject/folder and all its children are visible as soon as i leave that trigger it will become invisible: here is what i tried Code (csharp): var cull_this : UnityEngine.GameObject; function OnTriggerEnter (col : Collider) { cull_this.SetActiveRecursively(true); } function OnTriggerExit (col : Collider) { cull_this.SetActiveRecursively(false); } this works ! but comes with a roughly 3-6 seconds of lag when the room is being activated ... totally unacceptable so i tried to just toggle the renderer checkbox on all the children instead: Code (csharp): var cull_this : UnityEngine.GameObject; function OnTriggerEnter (col : Collider) { GameObject.GetComponentsInChildren.renderer.enabled = false; } function OnTriggerExit (col : Collider) { GameObject.GetComponentsInChildren.renderer.enabled = true; } but all this does is give me this error message: Assets/Scripts/Misc/culling_system.js(7,13): BCE0020: An instance of type 'UnityEngine.GameObject' is required to access non static member 'GetComponentsInChildren'. i don't understand this error message i thought i was using UnityEngine.GameObject ... what am i doing wrong ?!
GetComponentsInChildren is a function and it returns an array of objects The script reference on that commands might be helpfull to learn how to work with arrays Generally, the problem you have found in the first attempt is that you inject a mass of new game objects into the simulation in one go. Thats pretty heavy if dozens of collisions and renderers are just switched on from one moment to the next
i dont want hem to stop colliding or stop simulating while off i just don't want them to draw. all other game engines can do this without lag so i am sure unity can do aswell easily i just don't know with what command ^^
Why not simply enable/disable the renderer components, as opposed to the entire gameobject? This should be fast. If it still causes a lag, you might try batching it in a coroutine... doing a dozen (or so) at a time over the course of a couple seconds.
Another idea would be to set the camera's far clipping level such that when in one room the other room is not rendered (this only works if the rooms are far enough away from each other). Or... another idea would be to use two cameras. Have each set to render a certain set of layers that corresponds to particular room. Then simply toggle the cameras based on what room the player is inside.
thats what i tried with the second script but unity wouldnt let me but i dont understand the error message how can i recursively deactivate all renderers under a root node ?!
This code will disable all the renderers.... Code (csharp): var AllRenderers = GetComponentsInChildren(Renderer); for (var thisRenderer: Renderer in AllRenderers ) { thisRenderer.enabled = false; }
you are DA MAN ! this works like a charm ! so the full script that works now is: Code (csharp): var cull_this: UnityEngine.GameObject; function Start () { var AllRenderers = cull_this.GetComponentsInChildren(Renderer); for (var thisRenderer: Renderer in AllRenderers ) { thisRenderer.enabled = false; } } function OnTriggerEnter (col : Collider) { var AllRenderers = cull_this.GetComponentsInChildren(Renderer); for (var thisRenderer: Renderer in AllRenderers ) { thisRenderer.enabled = true; } } function OnTriggerExit (col : Collider) { var AllRenderers = cull_this.GetComponentsInChildren(Renderer); for (var thisRenderer: Renderer in AllRenderers ) { thisRenderer.enabled = false; } } in case some other code noob artist like me shows up and wants to optimize his scene )) HOW TO USE: - put all objects in one room into a group/folder/gameobject - make a triggerbox that encompasses all the area from where the room can be seen (probably the dimensions of the room itself and the most nearby corridors) - assign this script to the trigger box - in the exposed cull_this variable field choose the room DONE this manual-occlusion script is now public property of the unity community
Keep in mind that calling GetComponent or FindObject, etc. has a considerable amount of overhead. If the objects in question are all present at startup, then your Start function should assign the results of the GetComponent call to an array variable and then you can enable/disable renderers etc. by iterating on that array instead of doing the GetComponent call each time you want to switch things on and off.