- Home /
Cycling Texture Array
function Update () {
if(dialogue1)
{
dial1();
}
var dialogue1 : boolean = false;
public var dialTexture : Texture2D[];
function dial1 () {
renderer.enabled = true;
renderer.material.mainTexture = dialTexture[0];
yield WaitForSeconds(3);
renderer.material.mainTexture = dialTexture[1];
yield WaitForSeconds(3);
renderer.material.mainTexture = dialTexture[2];;
yield WaitForSeconds(3);
dialogue1 = false;
Debug.Log("Yay");
}
Hey guys, I'm trying to get textures to cycle in this function but when it switches to the second texture it begins to rapidly flick between the two, and then again with the third one. I just want it so that it shows the first texture in the array, waits 3 seconds, and then moves to the next.
Any help will be much appreciated, I'm not great with Unity.
Answer by robertbu · Jul 05, 2013 at 11:39 PM
The problem is that 'dialogue1' is staying 'true' for nine seconds since it only get set to false at the bottom of dial1(). During those nine seconds, the code starts in the range of 250 - 600 coroutines before the first one finishes ans sets dialogue1 to false. During the next nine seconds all those coroutines are fighting to set the texture. To solve your problem you can either move 'dialogue1 = false' to the top of the dial1() function, or you can move it into the Update():
if(dialogue1)
{
dial1();
dialogue1 = false;
}
Thank you very much, that solved everything. I thought it was something like that but I couldn't quite put my finger on it. Thanks again for your help!