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:
red to green
yellow to green instead of yellow to blue
The problem is probably in “instanced” materials…
I have noticed in Inspector that one material just disappeas.
Btw Swap routine works fine.
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[] 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;
}
}
1115250–42056–$cube.unitypackage (2.04 MB)