I need to convert a Random.Range value into a Byte so I can set a Sprites Opaqueness to be a random value.
This is my current code
sR.color = new Color32(255,255,255,Random.Range(50,200));
I need to convert a Random.Range value into a Byte so I can set a Sprites Opaqueness to be a random value.
This is my current code
sR.color = new Color32(255,255,255,Random.Range(50,200));
what about a simple cast?
sR.color = new Color32((byte)255, (byte)255, (byte)255, (byte)Random.Range(50,200));
Just casting the Random.Range is enough.
–Eric
ah, thats right… plane numbers between 0 and 255 can be interpreted as byte automatically.
Whats wrong with it the way it is?
I get this error:
UnityEngine.Color32.Color32(byte,byte,byte,byte) has some invalid arguments
The compiler isn’t smart enough to know that “Random.Range(50, 200)” will always return a value that can fit in a byte. So you have to tell it yourself.
–Eric
I know this is old but it is the top link on google. System.Convert.ToByte(yourInt) works and so does(byte)yourInt.
You can use this:
Random.Range(0, 255).ConvertTo<byte>()
it worked for me perfectly
Your code will never return 255. Read the docs for why.
You should write UnityEngine.Random.Range(0,256) if you want the full range possible in a byte