- Home /
Objects flashing for one frame after building for Windows
In our current project we are using a 3D book (MegaBook) which can be paged by touch/drag. I work on a 2013 Macbook Pro. When opening the front cover I also move the book smoothly a little to the left while dragging the page. Everything works fine until a do a build for Windows.
After the build the book moves for one frame to its end position resulting in a flashing object when I start a drag event. This is the case in the Windows build and in play mode!
The wrong position is due to a logic error in my script handling the touch-down event. The position resets for in a line before the correct position is set again when the touch down event is triggered.
What is troubling is Unitys unpredictable behaviour. When I start/restart Unity the scene in play mode works as expected, after doing another build the problem is there again. BUT: not always!! Sometimes the build and the play mode work as I expect. This makes it very hard to debug, it took me a long time to find out what the errors was. I do not use any threads.
In the same script I call a function while dragging. I return if a flag is not set. Yet I have to check the flag in the same function again because the value might have changed.
void OnDragging(DragInfo draginfo)
{
if (!isPaging) return; // only execute when flag is set
ray = camera.ScreenPointToRay(draginfo.pos);
Vector3 currentPos;
if (Physics.Raycast(ray, out hit, 1000f, touchLayerMask))
{
currentPos = hit.point;
}
else return;
// hasPagedAfterTouch = true;
float newPagePos;
canStopPaging = true;
// page forward
if (turnDir == 1)
{
// if current position is smaller than startPos compute newPagePos
if (currentPos.x < startPos.x)
{
..... Calculate new page
}
// page back
else if (turnDir == -1)
{
..... Calculate new Page
}
// Sometimes isPaging changed while function is executed
if (isPaging && hasPagedAfterTouch)
{
currentBook.SetPage(TranslateBookPage(nextPage), true);
}
}
Why does Unity sometimes draw the wrong position, sometimes it does not? Are functions called by events interrupted (threading?) and the frames drawn before the function is continued? I always assumed that a function is executed in one go.
Unity never draws the wrong position. Only what you give it.
If you want a function to defer execution you must use a coroutine.
If you move an object and on the next line move it back you should not see this movement as it was executed within one frame and does not get the chance to Update in between.
Use stepping and breakpoints to help find the problem. Debug.Log can also assist greatly in this.
Thank you for your input. I am aware that unity does not draw false positions if I do not set it that way. I mentioned in my question that it was mistake i had in my logic. The problem is that unity does under (at least for me) unpredictable circumstances draw the error, sometimes it does not.
The code is unchanged, but when the false position (set by me, I am aware) will only render after I do a build. That is puzzling.
By using Logging and other Debugging Tools I could confirm that I made an error, but the error shows only after doing a build. And even then it does not in all cases.
In this case, you should analyse the Branching. i.e the logic/conditions. You are right; it is hard to diagnose errors/bugs where conditional branching is involved (if else etc).
Use Block comments /* */
to remove chunks of code in the script until you are left with the bare bones, filling in some temporary dummy variables where things are required in order to do other functions. Otherwise Code-stepping is going to be your best friend.
Thanks you for taking time to answer! The problem is, I know I made an error and where it is. The error is always there, confirmed by logging and Reflecting the value in a debug tool.
But unity draws the wrongly set position when a value within a currently executed function changes for a line and is set to the correct value a few lines down. It does this only after compiling a build. Then the flickering object is drawn in the build and in play mode. After a fresh start of Unity the error does not show anymore. The error is always there, I found it by exactly the practices you mentioned. Only it does not always render.
To rephrase my question: does Unity step out of a currently executing function to draw a frame?
Your answer
Follow this Question
Related Questions
Fire an event when gameObject has been renamed? 2 Answers
IPointer Events don't seem to be working 3 Answers
Load xml from resource folder after build(web player) 0 Answers
huge iOS build size even with empty scene 0 Answers
Subsequent builds getting smaller.,Subsequent builds taking less space? 1 Answer