- Home /
preventing errors by calling Debug.Log on a property
Hi there. In my project I'm animating the position and field of view of a perspective camera. I also have a script that modifies an object's rotation in a LateUpdate.
Quaternion To Matrix conversion failed because input Quaternion is invalid {-0.073833, 0.885253, 0.000000, 0.777146} l=1.393081
The line of code that causes this is
transform.localRotation = originalRotation
This is fine, I know why it's giving me this error. Everything behaves as I expect as anyways. The interesting thing is that I can get this error message to go away just by calling this line of code first:
Debug.Log(transform.localRotation);
What could Debug.Log possibly be doing that fixes my quaternion?
I can't see why it would "fix" the error. It might just be that Unity is not spam$$anonymous$$g your error because you are actively logging and it doesn't want to drown your debugs with it. I don't know though, that's kinda weird.
Are you 100% sure that your transform.localRotation is still getting modified after putting the debug.log statement in first?
Yes. The code properly sets the rotation and there's really no difference in behaviour. It's also not a spammy error, it's more of an intermittent error maybe once every 5 - 10 seconds.
Answer by denisb · Oct 11, 2014 at 03:56 AM
it may be a race condition, when you dont call debug.log your originalRotation may be undefined, and if you do debug.log it takes some time and the originalRotation has been set meanwhile.
You may be right. However originalRotation is only set once in Awake(). I think the reference in question here is the actual transform.rotation. I figured that by doing this code in LateUpdate I would avoid having conflicts with other things modifying the transform. Perhaps I have some other LateUpdate doing things to it? I'll look into it.
That's a good idea @denisb. I totally didn't about it but I have seen race conditions become rarer or simply go away in some cases just by adding process intensive code to the code base.
The only thing I would say though is that LateUpdate is always going to happen after Update so I don't think that would cause problems. I would start by process of eli$$anonymous$$ation just removing the Debug.log so that the error comes up again and then removing transform modifications one by one until I can make sense of it.
I found a bunch of other things doing stuff in LateUpdate. I'm willing to guess here the race condition is because one is trying to change the position and one is trying to change the rotation. However since it's a perspective camera, changing the position in turn changes the rotation and this is what's fighting. I'll use coroutines to space them out over a frame or so.
Your answer
Follow this Question
Related Questions
how do I compare Quaternions? 6 Answers
Quaternion and 360 degree rotations 1 Answer
Rotate GameObject on click 1 Answer
Changing the speed of a rotating object 2 Answers
Quaternion rotation issue 1 Answer