Using coroutine and texture array to randomly associate textures?
Hi, I'm trying to create a talking animation within my Unity 3D game.
I was wondering how I could create a script so that the texture updates every 0.5 seconds to a random texture in an array when talking=true or something like that if it even makes sense.
Comment
Answer by Llama_w_2Ls · Jul 28, 2020 at 11:40 AM
bool isTalking;
Texture[] Textures = new Texture[] {/* Array of textures */};
GameObject playerMouth;
private void FixedUpdate()
{
if (isTalking == true)
{
StartCoroutine(ChangeTexture());
}
}
IEnumerator ChangeTexture()
{
yield return new WaitForSeconds(0.5f);
//Delay of 1/2 a second
int RandomTexture = UnityEngine.Random.Range(1, 3);
//Assuming there are 3 textures in the texture array
Texture texture = Textures[RandomTexture];
//You now have a random texture from your texture array
playerMouth.GetComponent<Texture>().Equals(texture);
//Sets the texture of the players mouth to a random texture twice a second
}
Ive never worked with textures before so the last line of the IEnumerator might need some work, but here goes.
Your answer
![](https://koobas.hobune.stream/wayback/20220613004229im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Hiding part of mesh 0 Answers
What is Coroutine, Yiel and IENumerator? 1 Answer
How can i make a tiled/striped texture on a sphere ? (see example below) 0 Answers
Apply texture/materials without stretching them according to the surfaces of the 3D structure 0 Answers
How can I reset the score counter? 0 Answers