Apply different textures to multiple materials

I have a folder, called “Walls” in the Ressorce-folder, which contains multiple textures, calling “wallRoi[1-26]”. Now I would like to apply those to my model in Unity via C# scripting.
Explaning my code: The model has multiple segmentation of the whole wall, each of them has a tag (“Wall[1-26]”). The tag of the whole wall is “Wall”. Now I’m trying to loop through the whole wall and apply to each segmenation of the wall a different texture from the folder. My Code doesn’t work, any purposes? Thank you!

    private UnityEngine.Object[] walltextures;
   
        void mapTexturesOverWalls() {
            walltextures = Resources.LoadAll ("Walls", typeof(Texture2D));
   
            Texture2D[] wallTex = (Texture2D)walltextures [walltextures.Length];
   
            GameObject walls = GameObject.FindGameObjectWithTag ("Wall");
            Renderer[] renders = walls.GetComponentsInChildren<Renderer> ();
   
            for (int i = 0; i < wallTex.Length; i++) {
                foreach (Renderer r in renders) {
                    r.material.mainTexture = wallTex[i];
                    UnityEngine.Debug.Log (wallTex + "");
                }
            }
        }

Use .sharedMaterial instead of .material property. With .material you created a copy of the renderer’s material. Oringinal material remains unchanged. Also check if the shader do actually have parameter for main texture - it may be named differently in the shader’s code and in this case default accessor .mainTexture will not work. Material class have methods for setting and getting parameters by names and default accessors like .mainTexture and .color are just aliases.

sharedMaterial doen’t really solve my problem and it wasn’t actually my problem. The question was how to loop over each wall segment and to map a diffrent texture to it.

If you leave it with .material, you’ll probably get a memory leak.

You code concept is wrong.
First, tag your wall segments the way tag strictly equals texture name. You need this because you don’t want to parse and rebuild strings, that’s a mess.
Then read all the textures to array as you do.
Create a dictionary of string, Texture and add all textures there using a texture’s name as a key.
Then iterate through renderers as you do, get a texture from the dictionary using renderer.gameObject.tag as a key, and apply it to the render.