Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

391
Vistas
Get all materials of parent and its children in an array except for few

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;
            
        }

    }

 }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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.

enter image description here

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);        
}
over 4 years ago · Santiago Trujillo Denunciar

0

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++;
    }

   
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda