PaulS:
The sensors are infrared sensors. It works once and after it goes through the loop it stops working!
No, it doesn't. It simply doesn't do what you expect. So, clearly your expectations are wrong.
I want the Udp.packets to be written only when the status of my sensors change. That's why I'm using an if statement. This is my expectation.