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.