Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

392
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!