Skip to main content

Posts

Showing posts from February, 2016

How to create unique ID in unity

Hi guys,

Sometime we come in a situation where we need to assign an unique id to our gameObject or any other thing.
Creating a random number is not reliable because there is a chance that 2 generated numbers can be same.
So i came up with a simple solution, in which i am generating a number by mixing date and time of the system, which will never be same, because it changes every micro second.

Here is the function to get Unique ID,

stringGetUniqueID()
{
string[]split= System.DateTime.Now.TimeOfDay.ToString().Split(newChar[]{':','.'});
stringid="";
for(inti=0;i<split.Length;i++){
id+=split[i];
}
returnid;
}



Or you can use  :
'System.Guid.NewGuid()' as suggested by 'Yed' in comments.



If you like this post and want to contribute Please click on the link given below.
https://www.paypal.me/Sushanta1991