Interesting error in my code

When I tried to write the tracking code I got what I think is an interesting error. Basically as soon as I ran the program the whole of Unity froze, with a high cpu usage. This was symptomatic of a runaway loop, which I tracked down to the Update call where i used a C# While loop to run until the balls position was at 1829 on the z axis, updating the camera position accordingly. However, I was a little puzzled as to why this was locking me out as the ball would eventually reach that position. Then it occurred to me that the Update call isn’t a simple function such as the one we code ourselves but is called by the system on every frame. Therefore as soon as the first frame is called the program went into a while loop waiting for the ball to reach 1829, however, since we were now locked in the first frame the ball couldn’t move any further. When I changed the While to an if statement as suggested in the video, everything ran OK.

1 Like

Privacy & Terms