- Home /
How can i change the gameobject color based on RGBA
I can get a string value RGBA(1.000, 1.000, 1.000, 1.000) from one gameObject
I know that it can change the gameObject color in runtime shown as below, but how can i make RGBA(1.000, 1.000, 1.000, 1.000) value to this gameObject?
GameObject.Find("Cube").transform.renderer.materials[0].color=Color.red
Answer by robertbu · Mar 15, 2014 at 04:07 AM
Unity has the Color class. In C# you would do:
GameObject.Find("Cube").transform.renderer.materials[0].color = new Color(1.0,1.0,1.0,1.0);
There is a constructor that takes three parameters so if you are not dealing with the alpha channel you can do:
Color c = Color(0.5, 0.2, 0.6);
Thank you @robertbu
In program$$anonymous$$g, how can i make the the 1.000 in different varable that the code become:
String color = RGBA(1.000, 1.000, 1.000, 1.000)
_red =
_green =
_blue =
_a =
GameObject.Find("Cube").transform.renderer.materials[0].color = new Color(_red,_green,_blue,_a);
Ahhh this is a string split and conversion problem. I did not understand that from your initial question. I tend not to answer these kinds of questions. Since I don't do much of them, my answers are brute force. There are many on this list that have very elegant solutions to these problem. So here is a brute force solution, but if you wait around for more answers, it is likely someone will give you a more elegant solution:
function Start() {
var colorString : String = "RGBA(1.000, 1.000, 1.000, 1.000)";
var start = colorString.IndexOf("(");
var length = colorString.IndexOf(")") - start - 2;
var s = colorString.Substring(start+1, length);
var nums : String[] = s.Split(","[0]);
var _red : float;
var _green : float;
var _blue : float;
var _a : float;
float.TryParse(nums[0], _red);
float.TryParse(nums[1], _green);
float.TryParse(nums[2], _blue);
float.TryParse(nums[3], _a);
Debug.Log(_red+","+_green+","+_blue+","+_a);
}
Note I'm still confused by your question. How do you get a string color value from a game object. Is RGBA() a class I'm unaware of or something else? If this is a Unity color, you should be able to access a Color class value on that game object.
Thank you, i will try the solution ASAP,. however, is it similar to C#?
btw, the RGBA(1.000, 1.000, 1.000, 1.000) String value is get by
itm.color = body.transform.renderer.materials[0].color.ToString();
i store RGBA(1.000, 1.000, 1.000, 1.000) in the X$$anonymous$$L string value, also , i need to load and read this value to set it into new gameobject. So, i need to change RGBA(1.000, 1.000, 1.000, 1.000)
Ahhhh, now everything makes sense. Here is the C# translation of the above code:
void Start() {
string colorString = "RGBA(1.000, 1.000, 1.000, 1.000)";
int start = colorString.IndexOf("(");
int length = colorString.IndexOf(")") - start - 2;
string s = colorString.Substring(start+1, length);
string[] nums = s.Split(","[0]);
float _red, _green, _blue, _a;
float.TryParse(nums[0], out _red);
float.TryParse(nums[1], out _green);
float.TryParse(nums[2], out _blue);
float.TryParse(nums[3], out _a);
Debug.Log(_red+","+_green+","+_blue+","+_a);
}
Thank you :> i will try it ASAP, because i have some problem for get the value form X$$anonymous$$L, i will try this after i can get the X$$anonymous$$L value.