Every item in a SharePoint list or library is assigned an auto-incrementing unique ID by default. Are you saying you need a completely separate unique ID that you create by yourself? What does it need to look like? Is there a reason you can't use the built-in unique ID? Answer those questions, and I should be able to give you a solution pretty easily.
My recommendation is to utilize the built-in ID field, and if you need that field in your form, then create a Receive data connection to your form library, set it for the Active Form only (checkbox), and set it not to automatically retrieve. In your submit button, add a rule with actions that first auto-generate a filename, then submit using that filename, then query the data connection to retrieve your ID, and set that ID value to a hidden field in your form, then submit again, and finally close the form. That way, on first submit, you let SharePoint create an ID, store it back in your form for later use, and re-submit the form.