Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

390
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda