Unity Day 2: Where I Learn how to make Random Colors

Blog Posts ,Programming ,Unity
October 30, 2014

Working “Randomly” is never easy. There is a lot that goes into a random number generator, and it tends to act just a little bit differently from language to language. Today, I learned the very valuable lesson that Random behaves just a little bit differently even in the same language, depending on the IDE.

For some background information, I’m used to the method in C# on Visual Studio where I have to initialize a Random object and then use that object to generate some nice numbers. It looks like this:

        Random r = new Random ();
        int i = r.Next ();

Today, I set out to expand on my fantastic Hello World Unity project and decided to make my fun little cube change colors on the mouse click. I expected it to take about two minutes.

What I found was an introduction to “the C# you thought you knew how to code in is actually going to be very different”, where I discovered that I didn’t actually have to initialize a Random object, and Next() was no where to be found. The first thing I tried was creating each color individually, once I found that I could call Range(min, max) directly on Random:

        float val1 = Random.Range(0255);
        float val2 = Random.Range(0255);
        float val3 = Random.Range(0255);
        Debug.Log (val1 + , + val2 + , + val3);
        Color newColor = new Color (val1val2val3);
        renderer.material.color = newColor;

That didn’t work – occasionally the cube would change color, but mostly it just stayed white. I found an old thread on the Unity forums about using renderer.material.setColor(), but that just threw weird errors. I finally went back to the basics and learned that in Unity, I could just use Random.Value();

        Color newColor = new ColorRandom.valueRandom.valueRandom.value1.0f );
        renderer.material.color = newColor;

Okay, great. This was pretty close, but the colors generated were all incredibly dark. I figured that, like most RNGs, the number being generated was between 0.0 and 1.0, so I tried scaling that with 255:

Color newColor = new ColorRandom.value255Random.value255Random.value*2551.0f );
        renderer.material.color = newColor;

This didn’t work, as the only color that would be generated was white. Here, I relied on trusty old trial and error, and eventually settled on a scaling factor of 10, which seemed to handle a wide range of colors nicely. The final code in the OnMouseDown method was refreshingly simple:

    void OnMouseDown() {
        Color newColor = new ColorRandom.value10Random.value
10Random.value*101.0f );
        renderer.material.color = newColor;

I did some digging, and I suspect that my problem with value generating was that the Color() class in unity doesn’t use integers, it uses floats. Having just finished up a stint as a wannabe designer, I was used to thinking of colors in their 0-255 values, but I suspect that was where I went wrong here.

For fun, I decided to do my first “dynamic” movement on mouse click and added a couple of lines using Random.Value() to move my cube around the screen when it was clicked.

Vector3 newLocation = new Vector3 (Random.value3Random.value3Random.value*3);
        transform.localPosition = newLocation;

Fun trick: add these lines of code to “Update()” to create a glitchy effect.

Related Posts

Leave a Reply