- Home /
 
 
               Question by 
               Jason Hamilton · Jul 12, 2011 at 08:05 AM · 
                texturematerialloop  
              
 
              Loop through textures every half second
Hey, Ive got to textures referenced in a script, and in that script i also have a gameObject referenced so i can edited the material at runtime. What id like to do is switch between those two textures every half second (thats just an idea, i would probably make it editable), how would I go about this?
               Comment
              
 
               
              Answer by Mortennobel · Jul 12, 2011 at 08:16 AM
You could something like this:
 using UnityEngine;
 using System.Collections;
 
 public class TextureLoopScript : MonoBehaviour {
     
     public float duration = 0.5f;
     public Texture[] textures;
     
     // Use this for initialization
     void Start () {
         StartCoroutine(DoTextureLoop());
     }
     
     public IEnumerator DoTextureLoop(){
         int i = 0;
         while (true){
             renderer.material.mainTexture = textures[i];
             i = (i+1)%textures.Length;
             yield return new WaitForSeconds(duration);
         }
     }
 }
 
              Answer by YikYikHeiHei · Jul 12, 2011 at 12:00 PM
I think it can help you
This is a javascript
 var waitTime : float = 0.5;
 var textures : Texture2D[];
 private var i : int = 0;
 // Use this for initialization
 function Update ()
 {
     Invoke("LoopTexture",waitTime);
 }
 function LoopTexture()
 {
     Debug.Log(i);
     renderer.material.mainTexture = textures[i];
     if (i < textures.Length-1)
         i ++;
     else
         i = 0;
     CancelInvoke("LoopTexture");
 }
 
              how would i do this if the object which the script is attached to has 2 materials? currently it is not doing anything to either thankyou
Your answer