Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Changing colors of a material creates blinding light

Discussion in 'Scripting' started by ElectronicSausage, Sep 5, 2023.

  1. ElectronicSausage

    ElectronicSausage

    Joined:
    Jun 19, 2023
    Posts:
    2
    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...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Drawing;
    4. using UnityEditor.Rendering;
    5. using UnityEngine;
    6. using UnityEngine.iOS;
    7. //Explicitly state which Color I'm using
    8. using Color = UnityEngine.Color;
    9.  
    10. public class BoxController : MonoBehaviour
    11. {
    12.     public float rotationSpeed = 10.0f;
    13.     public float horizontalInput;
    14.     public float verticalInput;
    15.     public int red;
    16.     public int green;
    17.     public int blue;
    18.     public int alpha;
    19.     public Renderer objectRenderer;
    20.     public Material objectMaterial;
    21.     public int currentBlue;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         //Initialize the color intergers and then the objects I'm changing
    27.         RandomColors();
    28.         objectRenderer = GetComponent<Renderer>();
    29.         objectMaterial = objectRenderer.material;
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         //Speed up rotations when I hold LeftShift
    36.         if(Input.GetKey(KeyCode.LeftShift))
    37.         {
    38.             rotationSpeed = 50.0f;
    39.         } else
    40.         {
    41.             rotationSpeed = 10.0f;
    42.         }
    43.        
    44.         //Move the cube around a bit
    45.         horizontalInput = Input.GetAxis("Horizontal");
    46.         transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime * horizontalInput);
    47.         verticalInput = Input.GetAxis("Vertical");
    48.         transform.Rotate(Vector3.right, rotationSpeed * Time.deltaTime * verticalInput); ;
    49.  
    50.         //Reset the cube's position when space is pressed
    51.         if (Input.GetKey(KeyCode.Space))
    52.         {
    53.             // When Space is pressed, create a reset position as a Vector3
    54.             // Then convert that Vector3 to a Quaternion and apply it to the transform's rotation.
    55.             Vector3 resetPosition = new Vector3(0, 0, 0);
    56.             transform.rotation = Quaternion.Euler(resetPosition);
    57.         }
    58.  
    59.         if (Input.GetKeyDown(KeyCode.Q))
    60.         {
    61.             //Populate the R, G, B, and alpha intergers
    62.             RandomColors();
    63.                  
    64.             //Create a new color with those values
    65.             Color adjustedColor = new Color(red, green, blue, alpha);
    66.            
    67.             //Set the base color to all 0's
    68.             objectRenderer.material.SetColor("_BaseColor", new Color(0, 0, 0, 0));
    69.            
    70.             //Three methods for setting the base color to the adjustedColor... none of them work correctly.
    71.             objectRenderer.material.SetColor("_BaseColor", adjustedColor);
    72.             //objectRenderer.material.SetColor("_Color", adjustedColor);
    73.             //gameObject.GetComponent<Renderer>().material.color = adjustedColor;
    74.  
    75.         }
    76.  
    77.  
    78.  
    79.     }
    80.     void RandomColors()
    81.     {
    82.         //Random.Range is max exclusive for the high end,
    83.         //so this should top out at 255 whole numbers
    84.         red = Random.Range(0, 256);
    85.         green = Random.Range(0, 256);
    86.         blue = Random.Range(0, 256);
    87.         alpha = Random.Range(0, 255);
    88.        
    89.     }  
    90.  
    91. }
    92.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,224
    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.
     
    Last edited: Sep 5, 2023
    Kurt-Dekker likes this.
  3. ElectronicSausage

    ElectronicSausage

    Joined:
    Jun 19, 2023
    Posts:
    2
    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.

    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.Q))
    2.         {
    3.             //Populate the R, G, B, and alpha intergers
    4.             RandomColors();
    5.                  
    6.             //Create a new color with those values and cast each int to a byte
    7.             Color32 adjustedColor = new Color32((byte)red, (byte)green, (byte)blue, (byte)alpha);
    8.            
    9.             // Methods for setting the base color to the adjustedColor
    10.             objectRenderer.material.SetColor("_BaseColor", adjustedColor);
    11.            
    12.  
    13.         }
     
    spiney199 likes this.