Missing Materials on build

I have 20 materials in an array that I am randomly choosing to apply to an object on instantiation. They appear fine when running the scene in the editor, but do not come through in the build.

I have tried both storing the Materials in an array (which results in magenta squares, i.e. missing materials) and also in a dictionary (which results in the default texture displaying, again, null materials).

Here are the code snippets I’m using. I’d appreciate any help with this, as I’ve been trying to sort this for a bit and can’t seem to pin it down. I’ve tried moving the materials to different locations in the Project window under assets/materials or assets/resources/materials, and this doesn’t seem to help.

  Public class Spawner : MonoBehaviour {

private Dictionary<int, Material> matDictionary = new Dictionary<int, Material>();
	
	void Start () {
    	getAllGalaxies();
	}
	
	void getAllGalaxies() { 
		allGalaxyMats = new Material[20]; 
		for(int i = 0; i < 20; i ++) { 
			Material galaxyMat = (Material) Resources.LoadAssetAtPath("Assets/Materials/Galaxy" + i + ".mat", typeof(Material)); 
			matDictionary.Add (i, galaxyMat);
       		allGalaxyMats *= galaxyMat;*
  •  }*
    
  • }*

  • void createFollowPoint() {*

  •  Vector3 tempLoc = new Vector3(0,0,0);*
    
  •  GameObject galaxy = (GameObject) Instantiate(Resources.Load("TUIOGalaxy"));*
    
  •  galaxy.transform.position = tempLoc;*
    
  •  int tempInt = Random.Range (0, 19);*
    
  •  Material tempMat = new Material(matDictionary[tempInt]);*
    
  •  galaxy.renderer.material = tempMat;*
    
  •  galaxy.renderer.material = allGalaxyMats[Random.Range (0, 19)];*
    
  • }*
    }
    I’ve included both the array and dictionary ways of doing things here, just so you might see any flaws in my thinking and help to point them out. Also, tempLoc is a variable that gets set elsewhere, but isn’t relevant to illustrate my point.
    Thank you in advance.

In case anyone ever finds this in the future and needs to know the answer to this, the solution was simple, and I’m disappointed no one suggested trying this. As a beginner, I realize there are many ways to do the same thing, but I don’t know what they all are.

So, instead of doing

for(int i = 0; i < 20; i ++) { 
     Material galaxyMat = (Material) Resources.LoadAssetAtPath("Assets/Materials/Galaxy" + i + ".mat", typeof(Material)); 
     matDictionary.Add (i, galaxyMat);
        allGalaxyMats *= galaxyMat;*

}
and basically loading the materials from Assets at runtime, it worked to create a public Materials array, and then just drag and drop all 20 materials into the array via the editor. Then, in the script, I access the materials array in the same way I did here in the script.
The difference is manually putting the materials in via editor vs. trying to load them from the assets folder at runtime.