- Home /
How to Use HSBColor script?
Hello,
I am trying to change the color of the object(it is a child object) via this script in this link: HSBColor Wiki but i couldn't call what i need to call to change color of my object in that script. I've just did that in my own script:
public HSBColor hsbcolor;
void Example()
{
this.transform.FindChild ("Graphics").GetComponent<SpriteRenderer> ().color = new HSBColor(new Color(1f, 2f, 3f, 1f);
}
Something is wrong in this code, i couldn't find what it is. I get error when i use this.
How can i call HSBColor function from script in link? Or what should i add after equal sign?
Answer by robertbu · Sep 22, 2014 at 02:16 PM
You have a few issues here. I'm guessing that you want to specify an HSBColor and assign the result to color the sprite. So you first have to produce an HSBColor struct with the right values, then you have to convert the result to a Color to be able to assign it. Untested:
transform.FindChild ("Graphics").GetComponent<SpriteRenderer> ().color = HSBColor.ToColor(new HSBColor(0.333,0.0f,0.5f,0.5f,1.0f));
roberthu, like you said, i need to produce a HSBColor struct with the right values but how? If you know how to do that, can you tell me, please?
Right now, i can't get the the color from hsb cause of the reason that you said i guess.
This:
new HSBColor(0.5,0.5f,0.5f,1.0f)
...produces the HSBColor struct. But I was wrong about the first value being 0 to 360. It is 0 to 1 like all the others. A quick mapping:
0 - red
0.333 - green
0.666 - blue
One more question. I tried to set colors with HSBColor but it was like setting color with this code:
transform.FindChild ("Graphics").GetComponent<SpriteRenderer> ().color = new Color(0.4f,0.1f,0,0.7f);
No differences. Is this HSBColor not working?
So the HSB settings for bright red would be (0,1,1,1). The RGB settings for bright red would be (1,0,0,1). If you are getting the same values, there is something wrong. I just ran a quick test of the HSBColor class, and everything worked correctly. In order to debug your problem, I need to see your exact code and have a description of 1) what you are trying to do, 2) your expectation, and 3) what is happening.
Answer by Julian_Nementic · Sep 22, 2014 at 03:31 PM
When you dont need HSB, than try this:
transform.FindChild ("Graphics").GetComponent<SpriteRenderer> ().color = new Color32(40,10,30,100);
//or this
transform.FindChild ("Graphics").GetComponent<SpriteRenderer> ().color = new Color(0.4f,0.1f,0,0.7f);
Your answer
Follow this Question
Related Questions
How to fill the inside volume of a 3D mesh with color? 0 Answers
Material doesn't have a color property '_Color' 2 Answers
How do I mix multiple colors into one color? 1 Answer
changing color on collision -1 Answers
Why doesnt unity find Hsb color? 1 Answer