- Home /
How to change material color to a specific object group's childs..?
This is the script I am currently Using.
I am trying to change all the child of the object group's color on a specific material.
This is what I have.
public Transform target;
public Texture2D colorPicker;
private Texture2D styleTexture;
public int textureWidth = 100;
public int textureHeight = 100;
public bool useDefinedSize = false;
public int positionLeft = 0;
public int positionTop = 0;
public bool useDefinedPosition = false;
public Color setColor;
public bool showPicker = false;
void Awake() {
//If size is not using defined size, resize to Texture Size.
if (!useDefinedSize) {
textureWidth = colorPicker.width;
textureHeight = colorPicker.height;
}
//If position is not using defined position, relocate to center of screen.
if (!useDefinedPosition) {
positionLeft = (Screen.width / 2) - (textureWidth / 2);
positionTop = (Screen.height / 2) - (textureHeight / 2);
}
//Set up color preview box.
styleTexture = new Texture2D(1, 1);
styleTexture.SetPixel(0, 0, setColor);
}
void OnGUI(){
//If picker is disabled, early return.
if (!showPicker)return;
//Color canvas.
if (GUI.RepeatButton(new Rect(positionLeft, positionTop, textureWidth, textureHeight), colorPicker)) {
//Get Mouse Position.
Vector2 pickpos = Event.current.mousePosition;
int canvasX = Convert.ToInt32(pickpos.x);
int canvasY = Convert.ToInt32(pickpos.y);
//Reposition Mouse input according to size and position of the canvas.
Color col = colorPicker.GetPixel(((canvasX-positionLeft)*((colorPicker.width/textureWidth))),(colorPicker.height-((canvasY-positionTop)*(colorPicker.height/textureHeight))));
//Return picked color.
Debug.Log(col);
//Set Color for display.
setColor = col;
}
//Apply button.
if (GUI.Button(new Rect(positionLeft + textureWidth - 60, positionTop + textureHeight + 10, 60, 25), "Apply")) {
//Change material color of all child in the target object.
foreach(Renderer r in target.parent.GetComponentsInChildren()) {
foreach(Material M in r.materials) {
M.color = setColor;
}
}
//target.renderer.material.color = setColor;
// hide picker
showPicker = false;
}
//Create Style for color preview.
GUIStyle style = new GUIStyle();
//Update Color for color preview.
styleTexture.SetPixel(0, 0, setColor);
styleTexture.Apply();
style.normal.background = styleTexture;
GUI.Box(new Rect(positionLeft + textureWidth + 10, positionTop + textureHeight + 10, 30, 30), new GUIContent(""), style);
}
}
I get this error :
Assets/Scripts/ColorPicker.cs(59,61): error CS0411: The type arguments for method `UnityEngine.Component.GetComponentsInChildren<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly
How do I fix this..? Can anyone point out my mistakes?
Comment
If the problem is on this line:
foreach(Renderer r in target.parent.GetComponentsInChildren()) {
It's because you aren't specifying the type and it can't be inferred. Try specifying the type of the component explicitly:
foreach(Renderer r in target.parent.GetComponentsInChildren<Renderer>()) {
Thanks for the quick-answer!
Its working very fine..! :D
I hope you have a nice day sir!
But how do I make it work only for a certain material?
I wanted it to only work for material #1 for example.
Best Answer
Answer by robhuhn · Nov 21, 2013 at 07:24 AM
You need to specify that you want a renderer component back:
target.parent.GetComponentsInChildren<Renderer>()