My one line of code crashes unity

For some reason, the following code crashes unity for me:

if (transform.localEulerAngles.y > 90f && transform.localEulerAngles.y < 270f) {
            transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, 180f, transform.localEulerAngles.z);
        }

As soon as my GameObject’s Transform is for example 180.001, Unity crashes. It freezes up with no error message and has to be forcefully terminated.

This occurs with both my previous version of 5.6.0f3 and with my current version 5.6.2f1.

Well, it’s weird that that crashes Unity, and you should file a bug report with your project so that UT can look into what’s happening there.

And after filing that bug report, you should trash these lines of code in favor of something that doesn’t use Euler angles. Not only will it avert this crash bug, Euler angles are just plain awful in general, and the posted code will give you all sorts of weird bugs in your game in the long run.

How did you determine that this particular line crashes Unity? Do you have a sample project for us to look at?

Where is that one line of code being called at? Is it the only thing in your project?

I’m genuinely surprised. You are saying an empty project with only this code crashes Unity?

Does this crash for anyone else?
The code is attached to an empty game object, which has a camera as a child object.

How can I provide a sample project for you all to look at?

Just create a project with the code/scene demonstrating the crash, zip up the project, and upload it here.

Oh… I thought maybe there was some fancy way I needed to do it.
Hardly anything there right now, so here the whole thing is. Right now it crashes after pressing ‘x’.

3125442–236735–Assets.zip (703 KB)

A lot of crashes are caused by memory leaks, and a lot of memory leaks are from infinite loops, although of course not all. But it’s a good starting point, to go check all your loops.
In CameraFlip.cs, I see you have this on line 45 :

        } else if (transform.localEulerAngles.y == 180f) {
            playerScript.reversedControls = false;
            while (transform.localEulerAngles.y != 0) {
                transform.localEulerAngles = (transform.localEulerAngles + tiltSpeed) * -1;
                light.transform.localEulerAngles = (light.transform.localEulerAngles + tiltSpeed) * -1;
                rotating = true;
            }
            rotating = false;
        }

I am suspecting this, as the way you have it, I don’t think that transform.localEulerAngles.y can ever be 0. So I’m suspecting (although just a guess, that you’re going to run into scenarios where it drops into this branch of the if, and it can’t ever evaluate the while condition to false to exit that loop.

2 Likes

I would at this point like to note the difference between a “crash” and a “freeze”. A crash means Unity exits immediately, probably with an error message. A freeze means it’s just stuck, and you have to force quit it yourself.

If it was a freeze, then @cstooch is most definitely right, as that loop looks absolutely prime freeze-causing material.

1 Like

That seems to have been it. Odd. I wonder then what the value was it ended up on…
Edit: some further testing reveals it was changing between 180 and 179. Indeed it never could reach 0.

I have done away with using Euler stuff now, as much as I can. Thanks for the advice and help on this, everyone.

Glad to help

Just some advice but you should not compare float using the equality operators. Instead you should use Mathf.Approximately.

1 Like