Home > Enterprise >  My PC freezes and crashes when calling this Void
My PC freezes and crashes when calling this Void

Time:07-13

Like the title says, my PC freezes and crashed when this void is called. I assume its spawning huge amounts of BrokenAircraft but I cannot figure out a fix. Heres the snippit:

    public void DestroyAircraft()
{
    Vector3 AircraftPos = Aircraft.transform.position;
    Destroy(Aircraft);
    GameObject.Instantiate(BrokenAircraft, AircraftPos, Quaternion.identity);
    foreach(Transform t in BrokenAircraft.transform)
    {
        Rigidbody rb = gameObject.AddComponent(typeof(Rigidbody)) as Rigidbody;
        //MeshCollider mc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
        
        if(rb != null)
        {
            rb.AddExplosionForce(Random.Range(minExplode, maxExplode), transform.position, radius);
        }
    }
}

CodePudding user response:

I don't know why / how that would crash the PC - I'd suggest diagnosing issues outside Unity with the PC (HD space, drivers, virus scans etc). However, Destroy and Instantiate are expensive processes, as is GetComponent. Better to enable / disable instead and use an object pool to cache resources: https://learn.unity.com/tutorial/introduction-to-object-pooling

public void DestroyAircraft()
{
    Vector3 AircraftPos = Aircraft.transform.position;
    Aircraft.SetActive(false);
    //GameObject.Instantiate(BrokenAircraft, AircraftPos, Quaternion.identity);
//Retrieve from object pool here instead
    foreach(Transform t in BrokenAircraft.transform)
    {
        //Rigidbody rb = gameObject.AddComponent(typeof(Rigidbody)) as Rigidbody;
        //MeshCollider mc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
// when object pooling these vars are already cached
        
        if(rb != null)
        {
            rb.AddExplosionForce(Random.Range(minExplode, maxExplode), transform.position, radius);
        }
    }
}

CodePudding user response:

I think this was the intended code, just make sure not to call this repeatedly - I guess once Aircraft is destroyed we better set it to "null"?:

public void DestroyAircraft()
{
    Vector3 AircraftPos = Aircraft.transform.position;
    Destroy(Aircraft);
    Aircraft = null;

    // Important: Get a GameObject reference of that new destroyed aircraft
    var destroyedAircraft = GameObject.Instantiate(BrokenAircraft, AircraftPos, Quaternion.identity);
    // Iterate over the destroyed aircraft, not its original prefab "BrokenAircraft"
    foreach(Transform t in destroyedAircraft.transform)
    {
        Rigidbody rb = gameObject.AddComponent(typeof(Rigidbody)) as Rigidbody;
        
        if(rb != null)
        {
            rb.AddExplosionForce(Random.Range(minExplode, maxExplode), transform.position, radius);
        }
    }
}

CodePudding user response:

So after testing a few times without the for loop, it still caused the issue. But I was able to Stop play before it froze entirly. There was an error saying I cannot destroy the GameObject because it was refrenced as public Transform instead of public GameObject swapping this fixed it. Thanks for the assistance from everyone :)

  • Related