- Home /
Question by
Mokenister · Feb 20, 2016 at 10:44 PM ·
texturematerialrender
c# - Change 3D texture on button press
I tried a lot of self-written codes but it never works. I even tried to change my player mesh on button press but it doesn't work either. So can someone please help me write this code because it's driving me crazy >.< Thank you ^^
Comment
Sure. Just post the code you wrote and others can see if they can help you with it.
Answer by Graphics_Dev · Feb 21, 2016 at 02:05 PM
Is this what you need?
using UnityEngine;
public class ChangeTexture : MonoBehaviour {
public int materialNum;
public Texture newTexture;
private Renderer myRenderer;
void Start () {
myRenderer = GetComponent<Renderer> ();
}
void Update () {
if (Input.GetButton ("Jump"))
{
if (myRenderer) {
// make sure material exists (if only 1 material materialNum must be 0)
myRenderer.materials[materialNum].mainTexture = newTexture;
}
}
}
}
Let me know if this helps ;)