#This surprisingly crashes my game

1 messages · Page 1 of 1 (latest)

fluid briar
#

can you show what you wrote, and how you executed it?

frosty gorge
#

yes — later tonight when i’m back home. thanks for the follow up

frosty gorge
#
private void Start()
{
    transform.LookAt(orb.transform.position);
    SpawnEnergy();
}

private async void SpawnEnergy()
{
    while (true)
    {
        // Cooldown
        try {
            await Task.Delay(Random.Range(delayMin, delayMax), destroyCancellationToken);
        }
        catch { }
            

        // Trajectory
        Vector3 locationOffset = Random.insideUnitCircle.normalized;
        Vector3 location = transform.position + locationOffset * spawnRadius;
        Vector3 direction = (transform.position - location).normalized;
        Vector3 force = direction * Random.Range(speedMin, speedMax);

        // Creation
        GameObject orbGameobject = Instantiate(orbPrefab, location, Quaternion.identity);
        Rigidbody orbRigidbody = orbGameobject.GetComponent<Rigidbody>();
        orbRigidbody.AddForce(force, ForceMode.Impulse);
        orbRigidbody.AddTorque(Random.insideUnitCircle * Random.Range(0.25f, 1.5f));
    }
}
#

It crashes when I get the TaskCancelledException, so when I stop running the game

#

I'm using Unity 2022.3.11f1

frosty gorge
#

Oh.

#

I need a break statement inside my catch, otherwise it keeps trying to run the rest of the code.