- Home /
 
Resources.FindObjectsOfTypeAll(typeof(Material))) not working correctly
I am working on a editor script to detect duplicate materials within a project and show this through a editor script. When I started the script Resources.FindObjectsOfTypeAll(typeof(Material))) would find all the materials within my project, leading to list of several hundreds off materials showing up. After I try'd to order this list through code the Resources.FindObjectsOfTypeAll(typeof(Material))) only seems to find the 18 materials pre- made by unity.
Why is this command suddenly acting different then before ? I tried going back a step and rewrite things, but that did not seem to help either. The basics if been working from was this script. Am I doing something wrong?
 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 
 public class DetectMaterialNew : EditorWindow
 {
     private Vector2 scrollPos;
     private bool groupEnabled;
 
     List<Material> arrayOfMaterials = new List<Material>();
 
     [MenuItem("Window/Detect Duplicate Material %g")]
     static void Init()
     {
         DetectMaterialNew window = (DetectMaterialNew)EditorWindow.GetWindow(typeof(DetectMaterialNew));
     }
 
     void OnGUI()
     {
         GUILayout.Label("Click to detect Duplicates", EditorStyles.boldLabel);
         if (GUILayout.Button("Detect Material"))
         {
             DetectAllMaterials();
         }
 
         if (groupEnabled)
         {
             scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
             EditorGUILayout.BeginHorizontal();
             EditorGUILayout.BeginVertical();
 
             GUILayout.Label("Total duplicate materials");
             for (int i = 0; i < arrayOfMaterials.Count; i++)
             {
                 EditorGUILayout.LabelField(i.ToString(), arrayOfMaterials[i].name);
             }
 
             EditorGUILayout.EndVertical();
             EditorGUILayout.BeginVertical();
 
             GUILayout.Label("Materials"); 
             for (int i = 0; i < arrayOfMaterials.Count; i++)
             {
                 EditorGUILayout.ObjectField(arrayOfMaterials[i], typeof(Material), true);
             }
 
             EditorGUILayout.EndVertical();
             EditorGUILayout.EndHorizontal();
             EditorGUILayout.EndScrollView();
         }
     }
 
     void DetectAllMaterials()
     {
         foreach (Material c in Resources.FindObjectsOfTypeAll(typeof(Material))) 
         {
             arrayOfMaterials.Add(c);
         }
         groupEnabled = true;
     }
 }
 
              Answer by d3ullist · Sep 18, 2014 at 09:01 AM
Seems like I could fix it fairly easily. This only occurred after I rebooted my unity, and after selecting certain materials in the hierarchy they would appear again.
With that knowledge I managed to fix it by adding the following part to the code.
     void Start()
     {
         Resources.LoadAll("Assets", typeof(Material));
     }
 
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Gizmos icons 0 Answers
Detect if build target is installed 1 Answer
Keep track of how many times the editor goes into play mode? 2 Answers