Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Is there a way to detect what object from a list throw exceptions and destroy this object ?

Discussion in 'Scripting' started by DubiDuboni, Jun 5, 2020.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    I have in the Hierarchy a parent and under the parent a lot of prefabs over 100.
    Some of the prefabs throw exceptions since they are missing components scripts or variables that was not assigned. I want some how when running the game in a script in Awake() to detect this prefabs and destroy them.



    Under Ammo Test I have a lot of prefabs in nested children and more.

    I can't just look for each one to check where and what is missing or wrong it will take too much time. I wonder if there is a way to make it automatic when running the game first time and to clean the "bad" prefabs ?

    I thought doing it already in the editor mode with editor script right after or when instantiating the prefabs but this seems even harder so maybe there is a way to do it in runtime using a script that will loop over the prefabs under Ammo Test and will destroy the prefabs that make the problems.
     
  2. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    This code is more or less closer to what I need :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ErrorsTester : MonoBehaviour
    7. {
    8.     void OnEnable()
    9.     {
    10.         Application.logMessageReceived += LogCallback;
    11.     }
    12.  
    13.     //Called when there is an exception
    14.     void LogCallback(string condition, string stackTrace, LogType type)
    15.     {
    16.        
    17.     }
    18.  
    19.     void OnDisable()
    20.     {
    21.         Application.logMessageReceived -= LogCallback;
    22.     }
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.        
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.        
    34.     }
    35. }
    36.  
    but the function LogCallback don't show the gameobject in the hierarchy that make the problem. I need to find the gameobject(prefab) name in the hierarchy so I can destroy it right on place inside the function LogCallback.