Creating 6 New Primitive Planes, with 6 different textures ... and they all display THE LAST TEXTURE

I was going to show you gents my code, but then I thought (a) they really don’t deserve that (it’s so bad, not going to inflict it upon you), and (b) … I’m not sure what part is the issue … so let’s get this narrowed down.

I have noise generation code that works. I can display it in the editor as a single tile, manipulate the value, see change, save and PNGs and they all do what I would expect.

I have code that takes these textures and applies them to in editor Planes so I can see them.

they all use the same - and LAST texture generated.

In the picture you can see the testing (output to pngs and assets) showing 6 distinct patterns.

I loop thru these, assign to planes … and get the same [last] texture.

I’ve been going thru my code and adding new() this and new() that, and nothing is working.

the textures sent into this method are, for sure, distinct (I tested it), but is there something in here that would cause all of my object to use the same texture???

The added arrays were simply an attempt to break the texture re-use. they can be removed. they didn’t work.

you can see the liberal use of new()

    public GameObject NewPlane(int x, int y, Vector3 pos, Vector3 size, Texture2D texture, string name)
    {
        MapGenerator mapGenerator = UnityEngine.Object.FindObjectOfType<MapGenerator>();
        planes = new GameObject[(int)mapGenerator.WorldTileDimensions.x, (int)mapGenerator.WorldTileDimensions.y];
        planes[x, y] = GameObject.CreatePrimitive(PrimitiveType.Plane);
        planes[x, y].name = name;
        planes[x, y].transform.position = pos;
        planes[x, y].transform.localScale = size;
        //plane.transform.localScale = scale;

        Material material = new Material(Shader.Find("Unlit/Color"));
        material.color = Color.white;
        Renderer planeRenderer = planes[x, y].GetComponent<Renderer>();

        planeRenderer.sharedMaterial.mainTexture = texture;
        planeRenderer.sharedMaterial.color = Color.white;

        return planes[x, y];
    }

would something

        for (int x = 0; x < WorldTileDimensions.x; x++)
        {
            for (int y = 0; y < WorldTileDimensions.y; y++)
            {
                worldTilesTextureMaps[x,y] = textureGenerator.TextureFromHeightMap(worldTilesNoiseMaps[x, y]);
                tilePosX = (x * TileSize) * 10;
                tilePosY = (y * TileSize) * 10;

                worldTiles[x, y] = mapDisplay.NewPlane(
                    x,
                    y,
                    new Vector3(tilePosX, 0, tilePosY),
                    new Vector3(TileSize - 1, 1, TileSize - 1),
                    worldTilesTextureMaps[x, y],
                    $"NoiseTile[{x}][{y}]");
                worldTiles[x, y].SetParent(noiseTileContainer,true);
            }
        }

in this cause the texture re-use?

I’ve already shared more code than I intended, as I am currently reverting this to an earlier version without the mass of “NEW!” and “ARRAYS!” …

I intended to just ask: Guys … what causes multiple objects to use the same texture, despite being given different ones?

You’re assigning to the .sharedMaterial. This is the material asset on disk that these meshes are using. So if you assign to it, they all change because they’re sourcing the same asset.

Also if you were actually using the new material you instantiate on line 11 of the first bit of code it wouldn’t be a problem. Right now you just make a new material… and do nothing with it.

.material is the per object instance material, and assigning to it will make a new material instance that you need to manage. Though you probably want to actually assign the material you instance (and modify first) to this, rather than mutate it directly.

Also don’t know what’s up with this attitude. Saying things like this will probably make folks less likely to help you. Be polite and respectful, or you’ll be left on your own.

1 Like

Hey, thank you!

Apologies, but English humor: “you don’t deserve that” as in “is freaking terrible, you’ve not done anything wrong to be subjected to my bad code” :slight_smile:

Nuance of the written language. My apologies for any perceived insult; not intended.

Your guidance is really really appreciated. I have many weaknesses, and assigning textures has to rank up there in them.

I will look into that section of my code!

I should add, I never would have figured this out. Like ever. Not sure I would have looked at that section of my code; I was so distracted and believed other areas were the cause. Thankyou.

1 Like