Hide unseen gameObjects

Hi, guys. I’m working on a project with a large building model so that there will be a large number of batches at runtime.
I want to know if there is any method to hide unseen gameObjects dynamically at runtime such as when you are in a room, and the gameObjects out of the room walls will be automatically hidden. Or let’s say that only the gameObjects strictly appeared in the main camera view will be shown at runtime.

What you want is Occlusion Culling.

It is not entirely clear to me what you want here. If objects are not in the main camera view, how can you see them? If you want to hide and show objects as you enter or leave rooms you can add a trigger to the entrance of rooms and then when someone passes that trigger, you have code that disables the objects in the room you came from and enable objects in the room you enter. You could also use raycasting to check if something is within the view of the player and then enable it if that is the case. Perhaps if you describe a bit more about what you want to achieve it will be easier for people to help you find a solution.

@jli262 I think I have found the solution to what your looking for.
Ok man so I seen this question last night and was intrigued so after a whole lot of searching I couldn’t find the answer so I came up with my own solution. Here it is.
Heres a link to my Github if you want to download the project

using System.Collections.Generic; // need this to use List<>
using UnityEngine;

// this script should be on your Camera and your camera should have a collider on it
// feel free to change, edit, distribute this code
// The main problems with this script is that if you have over 900 gameobjects in close proximity you 
get a slight lag if your paying attention/ not much
// On a lower end device this may cause more lag so take this into consideration
// maybe dont call this in an update loop but call it from a Coroutine or Event every 5 or 10 seconds 
with a big camera radius
public class CamMoveDistance : MonoBehaviour
// how far the camera will search for objects
float cameraSearchRadius = 30f;

// how close does the gameobject need to be before its visible
float closeDistance = 5f;
// how faraway from the camera does the gameobject need to be before turning off renderer;
float farDistance = 30f;
// this is the result of 2 vector3 subtracted from one another
float distanceBetween;
// List to store gameobjects we have prevoiusly hit with the camera
List<GameObject> hitObjs = new List<GameObject>();

// Camera movement Variables from Fuzzy Logic
// https://gamedev.stackexchange.com/questions/104693/how-to-use-input-getaxismouse-x-y-to-rotate-the-camera

public float speedH = 2.0f;
public float speedV = 2.0f;

private float yaw = 0.0f;
private float pitch = 0.0f;

// Update is called once per frame
void Update()
    // Scan from the position of the camera(assuming this script is attached to the camera)
    Collider[] hitColliders = Physics.OverlapSphere(transform.position, cameraSearchRadius); // Unity Docs OverlapSphere
    foreach (var hitCollider in hitColliders)
        // checks list to see if the current gameobject that was hit exists
        if (hitObjs.Contains(hitCollider.gameObject))
            Debug.Log($"Already contains {hitCollider.gameObject.name}");
            // adds hit gameobject to the list

    // searches the list of scanned gameobjects
    foreach(GameObject go in hitObjs)
        // makes sure the gameobject that is stored isnt the camera(assuming this script is attached to the camera)
        if (go != this.gameObject)
            // gets the distance between the camera(assuming script is attached to camera) and gameobject that has been prevouisly scanned
            distanceBetween = Vector3.Distance(transform.position, go.transform.position);
            // if the distnace between camera and gameobject is greater than the closeDistance
            if (distanceBetween > closeDistance)
                // turns on the gameobject renderer because it is farther than the closeDistnace from the camera and within the cameras Scanning radius
                go.gameObject.GetComponent<Renderer>().enabled = true;

                // You can substitute <Renderer> for the <MeshRenderer>
                // go.GetComponent<MeshRenderer>().enabled = true;
            if (distanceBetween > farDistance)
                // turns off the gameobject renderer because it is farther than the farDistance specified
                go.gameObject.GetComponent<Renderer>().enabled = false;

    // Got the camera mouse movement code from stack exchange
    // Username : Fuzzy Logic
    // https://gamedev.stackexchange.com/questions/104693/how-to-use-input-getaxismouse-x-y-to-rotate-the-camera
    yaw += speedH * Input.GetAxis("Mouse X");
    pitch -= speedV * Input.GetAxis("Mouse Y");
    transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);

    // basic camera movement
    if (Input.GetKey(KeyCode.W))
        transform.localPosition += new Vector3(0, 0, 1);
    if (Input.GetKey(KeyCode.S))
        transform.localPosition += new Vector3(0, 0, -1);
    if (Input.GetKey(KeyCode.A))
        transform.localPosition += new Vector3(-1, 0, 0);
    if (Input.GetKey(KeyCode.D))
        transform.localPosition += new Vector3(1, 0, 0);



If you have one huge building model imported is the case not that your building is one large game object? Print out the length of hitObjs in the code to see how many game objects it actually finds. Also, print the number of objects it actually hides to see if his code does anything to your scene.

If you have a building that is really large, another option is to use a multi-scene approach. You split your building into multiple models and place each model in its own scene. Then you can make the model static but load and unload scenes. As you get closer or further away from them.

See this example on how to use multiple scenes to handle large worlds Using Multiple Scenes - Unity3D.College

Or maybe there can be a way that the raycast from camera can cover the entire camera view so that I can set the cast gameobjects’ renderers’ enabled true.