Changing colors of a material creates blinding light

Hi all, total newbie here, just getting most of the way through the Junior Programmer track and loving it so far. I know PowerShell pretty well thanks to work, so this has been a really entertaining track!

My problem is that I’ve created a script that to change the color of the box to a random color when I hit Q. The random colors are fine, but they are so incredibly bright that the middle of the box turns white and the only color is visible as a corona around the burning white middle!

Can anyone tell me what I’m doing wrong or have misunderstood? I’ve read that colors multiply, so I tried zeroing out the colors, but that didn’t work. I’ve tried SetColor and material.color, but neither work. I’ve tried _BaseColor, _BaseMap, and plain _Color, but they either still create the blinding light or they do nothing. The goal is just to have a nice little box that spins around two axis (which it does fine!) and to change to some nice, calm random colors when I hit Q. Not sure what I messed up…

using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.iOS;
//Explicitly state which Color I'm using
using Color = UnityEngine.Color;

public class BoxController : MonoBehaviour
{
    public float rotationSpeed = 10.0f;
    public float horizontalInput;
    public float verticalInput;
    public int red;
    public int green; 
    public int blue;
    public int alpha;
    public Renderer objectRenderer;
    public Material objectMaterial;
    public int currentBlue;

    // Start is called before the first frame update
    void Start()
    {
        //Initialize the color intergers and then the objects I'm changing
        RandomColors();
        objectRenderer = GetComponent<Renderer>();
        objectMaterial = objectRenderer.material;
    }

    // Update is called once per frame
    void Update()
    {
        //Speed up rotations when I hold LeftShift
        if(Input.GetKey(KeyCode.LeftShift))
        {
            rotationSpeed = 50.0f;
        } else
        {
            rotationSpeed = 10.0f;
        }
       
        //Move the cube around a bit
        horizontalInput = Input.GetAxis("Horizontal");
        transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime * horizontalInput);
        verticalInput = Input.GetAxis("Vertical");
        transform.Rotate(Vector3.right, rotationSpeed * Time.deltaTime * verticalInput); ;

        //Reset the cube's position when space is pressed
        if (Input.GetKey(KeyCode.Space))
        {
            // When Space is pressed, create a reset position as a Vector3
            // Then convert that Vector3 to a Quaternion and apply it to the transform's rotation.
            Vector3 resetPosition = new Vector3(0, 0, 0);
            transform.rotation = Quaternion.Euler(resetPosition);
        }

        if (Input.GetKeyDown(KeyCode.Q))
        {
            //Populate the R, G, B, and alpha intergers
            RandomColors();
                 
            //Create a new color with those values
            Color adjustedColor = new Color(red, green, blue, alpha);
           
            //Set the base color to all 0's
            objectRenderer.material.SetColor("_BaseColor", new Color(0, 0, 0, 0));
           
            //Three methods for setting the base color to the adjustedColor... none of them work correctly.
            objectRenderer.material.SetColor("_BaseColor", adjustedColor);
            //objectRenderer.material.SetColor("_Color", adjustedColor);
            //gameObject.GetComponent<Renderer>().material.color = adjustedColor;

        }



    }
    void RandomColors()
    {
        //Random.Range is max exclusive for the high end,
        //so this should top out at 255 whole numbers
        red = Random.Range(0, 256);
        green = Random.Range(0, 256);
        blue = Random.Range(0, 256);
        alpha = Random.Range(0, 255);
       
    }   

}

UnityEngine.Color uses float values from 0 - 1. Color32 uses byte values, so 0 - 255.

I think you can work out where that’s going wrong in your script.

1 Like

AAAAAAAHHHHHHHHH!!! IT WORKS!!! Thank you! Now when I hit Q, I get random colors. This was the best thing to come home to after work. The kicker is that I actually read about Color32… but I was trying to access a method named Color32 ( so: objectRenderer.material.color32), I didn’t realize Color32 was a data type. Yesterday was a marathon day, so maybe my brain was faded.

I learned to cast data types , and I learned to pay better attention to the difference between Constructors and Classes.

Here’s my fixed code, in case some fellow nerd reads this thread in four years. I’m betting there’s a way to randomize a byte directly, but that’s Thursday night’s or Sunday afternoon’s project! Thank you again.

        if (Input.GetKeyDown(KeyCode.Q))
        {
            //Populate the R, G, B, and alpha intergers
            RandomColors();
                 
            //Create a new color with those values and cast each int to a byte
            Color32 adjustedColor = new Color32((byte)red, (byte)green, (byte)blue, (byte)alpha);
           
            // Methods for setting the base color to the adjustedColor
            objectRenderer.material.SetColor("_BaseColor", adjustedColor);
           

        }
1 Like