So this code is crashing Unity only sometimes...
So I've narrowed it down to the while loop by commenting it out and testing it. So I have no idea why but some times it works in play mode every time, then I exit play mode, re enter play mode and it freezes up and I have to manually end the application.
if (BlueSquareClicked == true && Physics.Raycast (camRay, out hit, camRayLength, BlueMoveMask))
{
Debug.Log ("Clicked on BlueMoveTile");
startPoint = transform.position;
endPoint = hit.transform.position;
BlueSquareMoving = true;
float journeyLength = Vector3.Distance (startPoint, endPoint);
float distCovered = (Time.time - startTime) * moveSpeed;
float journey = distCovered / journeyLength;
while (BlueSquareMoving == true)
{
Debug.Log ("Here");
//BlueSquareMoving = false;
transform.position = Vector3.Lerp (startPoint, endPoint, journey);
//Debug.Log ("Moving");
if (transform.position == endPoint)
{
Debug.Log ("Finished Moving");
BlueSquareMoving = false;
BlueSquareDoneMoving = true;
BlueSquareClicked = false;
GameManager.killTiles = true;
}
}
}
Any Help would be awesome. If I need to post the entire script let me know.
likely to be floating point errors meaning that transform.position never exactly equals endPoint. You could make that an approximate check, i.e. if the distance between them is less than something very small, finish.
Alternatively you could check when (journey >= 1)
and it might be more successful
@Scribe Idk I'm new to the way this website works... I think this will go in the right place...
The only problem with accepting and stopping before that exact number is that when the piece moves again it will be slightly off course and eventually will be out of line? The game works similar to chess, I'm still ironing out the turnbased movement system.
You can avoid that problem by calculating your endPosition based off of the central position of the square the piece should be in, rather than by calculating it from an offset of your start position. It looks like this is what you already do, so you should have no issues with it being slightly offset from the actual position.
Though I doubt you would notice any issues doing calculations the other way with any normal length chess game (you probably won't see a difference of 1E-7)!
You were correct first of course but I didn't quite get what you were saying until @sharat replaced the loop function with an if function and what you had suggested. Thanks for the help!
Answer by sharat · Sep 02, 2015 at 07:46 PM
Shouldn't while (BlueSquareMoving == true)
be an if statement? There are two cases for what's happening in the loop. Either journey >= 1
and it's run exactly once since BlueSquareMoving
gets set to false. Or journey < 1
and it will keep on setting transform.position to the same value over and over in an infinite loop(causing your freeze). I'd replace that loop with:
if(BlueSquareMoving == true)
{
transform.position = Vector3.Lerp (startPoint, endPoint, journey);
if (journey >= 1.0f)
{
BlueSquareMoving = false;
BlueSquareDoneMoving = true;
BlueSquareClicked = false;
GameManager.killTiles = true;
}
}
Your answer
Follow this Question
Related Questions
Timeline causes IOS app to crash when adding it, how do i fix this/ 0 Answers
Application.TickGlobalCallbacks causing crashing! 0 Answers
Android App Crashes Randomly 0 Answers
My game crashes with the following logs,Please help me out. 0 Answers
"If statement" and whiled loop acting strange and crashes 1 Answer