fix the c# script script below to be more optimized so it wont crash unity also keep in…
- User
fix the c# script script below to be more optimized so it wont crash unity also keep in mind I have UnityMainThreadDispatcher in scene and project.
using UnityEngine; using System.Collections.Generic;
public class DestroyDisabledMeshRenderers : MonoBehaviour { public bool Nuke = false; public Transform MasterParent;
// Create a list to hold all GameObjects in the hierarchy public List<GameObject> allGameObjects = new List<GameObject>(); public void NukeToggle() { Nuke = true; } public void Update() { if (MasterParent == null) { if (GameObject.FindGameObjectWithTag("TerrainOBJ")) { MasterParent = GameObject.FindGameObjectWithTag("TerrainOBJ").transform; } } if (Nuke && MasterParent != null) { // Gather all GameObjects in the scene GatherAllGameObjects(allGameObjects); // Check every GameObject in the list foreach (GameObject gameObject in allGameObjects) { // Check if the GameObject is a child of MasterParent if (IsChildOfMasterParent(gameObject.transform, MasterParent)) { MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>(); // Check if the MeshRenderer is not enabled if (meshRenderer != null && !meshRenderer.enabled) { // Destroy specific colliders DestroyCollider<MeshCollider>(gameObject); DestroyCollider<BoxCollider>(gameObject); DestroyCollider<Collider>(gameObject); // Will cover any other collider } } } Nuke = false; // Reset Nuke after processing } } // Gather all GameObjects in the scene (not limited to a parent) void GatherAllGameObjects(List<GameObject> gameObjects) { // Find all GameObjects in the scene GameObject[] allObjects = GameObject.FindObjectsOfType<GameObject>(true); gameObjects.AddRange(allObjects); } // Check if the game object is a descendant of the specified parent private bool IsChildOfMasterParent(Transform child, Transform parent) { while (child != null) { if (child == parent) return true; // Found the parent child = child.parent; // Move up to the parent } return false; // Parent not found in the hierarchy } // Helper method to destroy colliders of specific type private void DestroyCollider<T>(GameObject gameObject) where T : Collider { T collider = gameObject.GetComponent<T>(); if (collider != null) { Destroy(collider); } }}
- Assistant