[Solved] Converting variable types to differrent var types

I am attempting to do the random challenge but i prefer to use Network.time to generate a random seed as it calls on the time on the network or some function that calls on the time of the computer system.

the problem I am running into is needing to convert the Network.time value (which is of type double) and converting to type int and/or type float.

I am using a simple test process by declaring:
double d = 5000.07;
int IBS;

Now i need the function call to convert d to IBS, which will yield 5000 to be stored in IBS.

Then print to screen d and IBS to confirm success

I have looked through the unity API and have found nothing relatable so far. I have also googled and Microsoft has this https://www.tutorialspoint.com/csharp/csharp_type_conversion.htm

So far I have not been able to get anything to work.

Got it to work. Had to add “using system”; at top of script. Then use IBS = Convert.ToInt16(d); to make conversion. Conversion options are listed in the prior link.

If the desired result is to have IBS = 5000 then presumably you are not interested in the decimal?

Assuming so, you could use a cast;

IBS = (int)d;

that will just truncate the value losing the decimal.

Alternatively, if you are interested in rounding then;

IBS = Convert.ToInt32(d);

Hope this helps.

Thank you. It took me awhile but I finally came across a resource to use. https://www.tutorialspoint.com/csharp/csharp_type_conversion.htm

On a note though. the (int)d; option never worked for me. Also I had to add using system at the top of script to be able to use the convert option.

I should have mentioned about the other namespace, sorry. You aren’t limited to only the Unity ones obviously, all of the .Net ones are available also.

Glad you got it working anyway :slight_smile: