3

Every second creating an object and I need to change the color of every next object by rainbow. I know how to change one`s color object, but I need an algorithm to do that right

I think its not good to handy make each color code every time

Color32 color = new Color(0.5f, 1f, 0.5f, 1f); // good

but I need for example 1st object 255,190,0, next 230,255,10, I mean color circle repeating. What should I do?

enter image description here

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
Koma
  • 140
  • 9
  • 1
    What have you tried so far? We can try to fix your code but stack is not a code writing service – zambari Apr 16 '19 at 11:28
  • Possibly related: https://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both – Codor Apr 16 '19 at 13:07
  • 3
    Possible duplicate of [How do I get a rainbow color gradient in C#?](https://stackoverflow.com/questions/2288498/how-do-i-get-a-rainbow-color-gradient-in-c) – Johnny Apr 16 '19 at 13:10
  • I did something like this while ago, I'm writing solution, till then you can play from [Play-Store](https://play.google.com/store/apps/details?id=com.TeamZerothBD.MazyBall) – Maifee Ul Asad Apr 16 '19 at 14:26

2 Answers2

2

In order to have the colors enumerated in a "rainbowish" sequence, you would need to cycle the hue in the HVS color model and convert to the RGB model to initialize the new object. Both models as well as the conversion are described in this Wikipedia article.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • great! Thats what i was looking for! Thanks you. `m_Renderer.material.color = Color.HSVToRGB(m_Hue, m_Saturation, m_Value);` – Koma Apr 17 '19 at 13:14
-1

First make something simple, make it linear.

First approach :

    public GameObject target;
    public int n=50;


    void Awake(){

        for(int i=0;i<n;i++)
        {
            GameObject tem=Instantite(target,new Vector3(i,0,0),Quanterion.Identity);
            if(i/7==0)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.violate;
            }
            else if(i/7==1)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.blue;
            }
            else if(i/7==2)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.indigo;
            }
            if(i/7==3)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.green;
            }
            if(i/7==4)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.yellow;
            }
            if(i/7==5)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.orange;
            }
            if(i/7==6)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.red;
            }
        }


    }

This will produce a linear strip for us.

Now the final one. // Also gives rainbow shape, maybe you didn't ask

    public GameObject target;
    public int n=50;
    public int r=50;//try adjusting this one, this value can also be achieved doing some calculation but I forgot them

    void Awake() {
        float deg=0;
        for(int i=0;i<n;i++)
        {
            if(i!=0)
                deg=360/(float)i;
            GameObject tem=Instantite(target,Vector3(r*Mathf.Deg2Rad*cos(deg)/2, r*Mathf.Deg2Rad*sin(deg)/2,0),Quanterion.Identity);
            if(i/7==0)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.violate;
            }
            else if(i/7==1)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.blue;
            }
            else if(i/7==2)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.indigo;
            }
            if(i/7==3)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.green;
            }
            if(i/7==4)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.yellow;
            }
            if(i/7==5)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.orange;
            }
            if(i/7==6)
            {
                tem.GetComponent<MeshRenderer>().material.color=Color.red;
            }
        }


    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86