I am getting all materials from the parent and its children and storing it in an array. I want to however ignore few gameObjects from the list to be added. Can I add a string as the gameObject's name to be ignored so that except these gameObject, everything gets added?
Another thing is, how do I get all and each material from each gameObject? Few gameObjects have 5 and others have only 1. With the below code, I could only receive only one material from each gameObject.
public Renderer[] allChildRenderers;
public Material[] allMaterials;
public string[] GameObjects_ToIgnore;
void Start () {
allChildRenderers = GetComponentsInChildren<Renderer> ();
allMaterials = new Material[allChildRenderers.Length];
for (int i = 0; i < allChildRenderers.Length; i++) {
allMaterials[i] = allChildRenderers[i].GetComponent<Renderer> ().material;
}
}
}
If you want to ignore some GameObjects, you should use tags, which is a little harder to change than the name of the GameObjects themselves (this means your code may break if you change the name of the GameObject, as for the tag, you won't be changing frequently). Give the objects that should be ignored a new tag to indicate it.
for (int i = 0; i < allChildRenderers.Length; i++) {
if(allChildRenderers[i].gameObject.CompareTag("ignored_tag")) continue;
allMaterials[i] = allChildRenderers[i].material;
}
There is also a little ambiguity in your code:
allMaterials[i] = allChildRenderers[i].GetComponent<Renderer> ().material;
You are calling GetComponent<Renderer> in an object that is already a Renderer (allChildRenderers[i]). You should just call
allMaterials[i] = allChildRenderers[i].material;
As for the multiple materials in the same object, use allChildRenderers[i].materials to get the array of materials the object is using. allChildRenderers[i],material returns just the first material, while allChildRenderers[i].materials return the array with all of the used materials.
Try this:
First, change allMaterials for a list instead of array:
public List<Material> allMaterials = new List<Material>();
Then, try this:
for (int i = 0; i < allChildRenderers.Length; i++) {
if(allChildRenderers[i].gameObject.CompareTag("ignored_tag")) continue;
foreach(Material mat in allChildRenderers[i].materials)
allMaterials.Add(mat);
}
Yes
List<string> excludedNames;
int materialIndex = 0;
for (Renderer currRenderer in allChildRenderers){
if(!exludedNames.Contains(currRenderer.gameObject.name)){
foreach(Material currMaterial in currRenderer.materials){
allMaterials[materialIndex] = currMaterial;
materialindex++;
}
}
}
EDIT if allMaterials is supposed to have an entry per Renderer, it should be Material[][] or Renderer[]
Material[][] allMaterials;
List<string> excludedNames;
int rendererIndex = 0;
for (Renderer currRenderer in allChildRenderers){
if(!exludedNames.Contains(currRenderer.gameObject.name)){
int materialIndex = 0;
foreach(Material currMaterial in currRenderer.materials){
allMaterials[rendererIndex][materialIndex] = currMaterial;
materialindex++;
}
rendererIndex++;
}
}