How do I Change Texture in C#

Hello,

I have been trying to change the current texture of my game object to another texture. I looked this up in the references and nothing is working properly.

So far I tried both

renderer.sharedMaterial.SetTexture(“_MainTex”, StaffPeasant);

renderer.material.mainTexture = StaffPeasant;

but neither work. The name of the actual texture is StaffPeasant and I want to replace my current texture with that texture.
How would I do this?

Thanks for any help!

You need to supply a Texture variable, not the name of a texture. Typically you make a public variable and drag the texture onto it in the inspector.

–Eric

Oh okay thanks!
So am I going to need another texture variable for every texture or is there a better way to do that?
This is for the players equipment so I might end up with over 100 texture variables and I wasnt sure if that will slow the game down.

How would I change the material on GameObjects from this script that the script isnt attached to?

I tried
PlayerShieldFront.renderer.material.mainTexture = ShieldFrontPeasant;
but it does work

PlayerShieldFront is the name of my game object. This script is attached to my Player game object.

Never mind I figured it out, I just had to write GameObject and the name at the top, then drag in the game object to the slot in Unity.

From a Script: PlayerShieldFront.renderer.material = NewMaterial OR PlayerShieldFront.renderer.materials[index] = NewMaterial

If you want shared material:
PlayerShieldFront.renderer.sharedMaterial = NewMaterial
PlayerShieldFront.renderer.sharedMaterials[index] = NewMaterial