- Home /
Change textures at runtime on object with multiple textures?
I have a cube with 3 textures (green, black and blue). I'm trying to make it so the original textures (green, black and blue) on my mesh (cube) change every 5 seconds. Only lateral textures will be changed (green to yellow, blue to red). After 2 seconds yellow and red textures will be changed again to original texures. I have a problem when new textures come back to original.. The problem is probably in "instanced" materials.. I have noticed in Inspector that one material just disappeas. Btw Swap routine works fine.
public class Change: MonoBehaviour
{
public float coldTimer = 5.0f;
public float warmTimer = 2.0f;
public bool warming;
public Transform myCharacter;
public Texture oldTexturev1;
public Texture newTexturev1;
public Texture oldTexturev3;
public Texture newTexturev3;
private Material[] sharedMaterials;
private Material[] myMaterials;
private Material[] myMaterials1;
private Material tmp;
private float currTime;
void Start()
{
currTime = coldTimer;
warming = false;
myMaterials = myCharacter.renderer.materials;
myMaterials1 = myCharacter.renderer.materials;
//Debug.Log(myMaterials[0]);
//Debug.Log(myMaterials[2]);
}
void Update()
{
if(currTime > 0.0f)
{
currTime -= Time.deltaTime;
}
else
{
if(warming)
{
currTime = coldTimer;
tmp = myMaterials[0];
myCharacter.renderer.material = tmp;
myCharacter.renderer.material.SetTexture("_MainTex", oldTexturev1);
tmp = myMaterials[2];
myCharacter.renderer.material = tmp;
myCharacter.renderer.material.SetTexture("_MainTex", oldTexturev3);
warming = false;
}
else
{
currTime = warmTimer;
tmp = myMaterials[0];
myCharacter.renderer.material = tmp;
myCharacter.renderer.material.SetTexture("_MainTex", newTexturev1);
tmp = myMaterials[2];
myCharacter.renderer.material = tmp;
myCharacter.renderer.material.SetTexture("_MainTex", newTexturev3);
myCharacter.renderer.materials = myMaterials1;
warming = true;
//Swap();
}
}
}
void Swap() {
tmp = myMaterials[0];
myMaterials[0] = myMaterials[2];
myMaterials[2] = tmp;
myCharacter.renderer.materials = myMaterials;
}
}
Download package here: http://forum.unity3d.com/threads/163146-Change-textures-at-runtime-on-object-with-multiple-textures
Answer by potter3366 · Dec 21, 2012 at 09:45 PM
SOLVED!
using UnityEngine; using System.Collections;
public class Change: MonoBehaviour {
public float coldTimer = 5.0f;
public float warmTimer = 2.0f;
public bool warming;
public Transform myCharacter;
public Texture oldTexturev1;
public Texture newTexturev1;
public Texture oldTexturev3;
public Texture newTexturev3;
private Material[] myMaterials;
private Material[] myMaterials1;
private float currTime;
void Start()
{
currTime = coldTimer;
warming = false;
myMaterials1 = myCharacter.renderer.materials;
myMaterials = myMaterials1;
myCharacter.renderer.materials = myMaterials;
}
void Update()
{
if(currTime > 0.0f)
{
currTime -= Time.deltaTime;
}
else
{
if(warming)
{
currTime = coldTimer;
myCharacter.renderer.material = myMaterials[0];
myCharacter.renderer.material.SetTexture("_MainTex", oldTexturev1);
myCharacter.renderer.materials = myMaterials;
myCharacter.renderer.material = myMaterials[2];
myCharacter.renderer.material.SetTexture("_MainTex", oldTexturev3);
myCharacter.renderer.materials = myMaterials;
warming = false;
}
else
{
currTime = warmTimer;
myCharacter.renderer.material = myMaterials[0];
myCharacter.renderer.material.SetTexture("_MainTex", newTexturev1);
myCharacter.renderer.materials = myMaterials;
myCharacter.renderer.material = myMaterials[2];
myCharacter.renderer.material.SetTexture("_MainTex", newTexturev3);
myCharacter.renderer.materials = myMaterials;
warming = true;
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613085717im_/https://answers.unity.com/themes/thub/images/avi.jpg)