Changing color through Script

I have the code so every update, three integers (R, G, and B) will be changed to a value between 0 and 255. Am I not able to turn a tuple using these numbers (and transparency) into a color value?

That’s a great question! Are you not able to do this?

Unfortunately, we can’t read your mind and we can’t see your screen.

This may help you form a coherent basis for technical communication with strangers on the internet:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • ONLY post the relevant code, and then refer to it in your discussion.

Color() accepts channel params from 0.0f to 1.0f

Color32() accepts channel params from 0 to 255

More on Color vs Color32, lossless, comparisons, etc.

https://discussions.unity.com/t/891296/2

I want the program to constantly change the color of a sprite.
I tried to use script to constantly get random numbers and put the numbers into a color variable.

Code I used:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class color_change : MonoBehaviour
{
    private SpriteRenderer rend;
    private int R, G, B;
    public Color colorfull;


    // Update is called once per frame
    void Update()
    {
        R = Random.Range(0, 255);
        G = Random.Range(0, 255);
        B = Random.Range(0, 255);
        colorfull = (R, G, B, 255);
        rend = GetComponent<SpriteRenderer>();
        rend.color = colorfull;
       
    }
}

It returned: error CS0029: Cannot implicitly convert type ‘(int R, int G, int B, int)’ to ‘UnityEngine.Color’

Documentation I used: SpriteRenderer.color , Color

(is this good?)

Then go FIX IT! Here’s how:

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

This is not valid C#:

Go look at the constructor for the Color() struct. It has example code.

It also takes values from 0.0 to 1.0

Perhaps you seek the Color32() struct?

I had a similar problem, for some weird reason you can’t modify Color32 while the game is running. Just use Color that has values of 0.00f ~ 1.00f, and the code will work just fine. :slight_smile: