- Home /
Instantiated objects are all white?
Hi all,
So I'm currently wanting my instantiated object to have a random colour when it's being created, so the code is like:
if (Input.GetMouseButtonUp (0) && !IsPointerOverUIObject ()) {
int r = UnityEngine.Random.Range (0, Center.Count);
centerPiece = Instantiate (Center [r], new Vector3 (0, 0, 0), Quaternion.identity) as GameObject;
Debug.Log ("R: " + red + " G: " + green + " B: " + blue);
centerPiece.GetComponent<MeshRenderer> ().material.color = Color.red;
}
But currently the object all appear to be white when they are instantiated, or else for some reason locked in a cmyk colour; i.e. when there is a colour being reproduced.
Is this a matter of problem with my code, or how I've set up my unity project...? Could any one give me a bit of help?
Are your red, green, and blue variables in a 0-1 range or a 0-255 range?
If it's 0-1, then the color is being applied correctly and the problem lies elsewhere in your script.
If it's 0-255, then try using Color32 to define your color ins$$anonymous$$d. It will still be applied in the same manner.
Note how it says
centerPiece.GetComponent<$$anonymous$$eshRenderer> ().material.color = -->Color.red<--;
in the last line. I doubt it has to do anything with what format the color is in.
Answer by Arshia001 · Jul 23, 2016 at 04:45 PM
Your code isn't complete, so I can only guess:
Does your instantiated object contain only one mesh renderer? Is it located on the root GameObject, or is it further down the hierarchy?
Does the shader you use support colors? Mobile shaders don't support colors, only textures. Custom shaders may also exclude support for colors.
Answer by Cross_J · Jul 24, 2016 at 09:20 AM
I should've changed that @Arshia001 sorry... i was meant to be new Color (red, green, blue)
where I made the random range from 0-255.... I'm trying to make it use the Color32 but looks like I'm having problems with a phrase error as my random range is using float but it seems like unity wants a byte value... That's the only thing I'm trying to get my head around, and reading around the Unity Docs and API. Also thanks for the tip on that Color32 thing @Eno-Khaon
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Modifying a prefab 0 Answers
Instantiate buttons for different actions 0 Answers
How to instantiate RTS units properly 1 Answer
Instantiate Object At Local Position 2 Answers