I am trying to change the texture of a model at runtime so that it looks like animating,as my model is not animated.I have written a few line code which is working properly,its changing the image at runtime and my model looks like its animating.but the problem is that I dont know why but whenever I am playing it its strucking.I mean it stops for fraction of second and then play again it stops and then play.I am attaching the Script here.
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public MovingScore MovingScore = null;
public float gravity = 9.8f;
public static int Score = 0;
//public Texture img;
bool isEggClicked = false;
int isImageChanging ;
public Texture[] image1 = new Texture[4];
int index = 0;
public ParticleEmitter hundred0ne;
// Use this for initialization
void Start () {
isImageChanging = 1;
image1[0]=Resources.Load("Gready 21") as Texture;
image1[1]=Resources.Load("Gready 22") as Texture;
image1[2]=Resources.Load("Gready 23") as Texture;
image1[3]=Resources.Load("Gready 22") as Texture;
}
// Update is called once per frame
void Update () {
if(isImageChanging == 1){
index = 0;
//StartCoroutine(ChangeTheImage());
}
else if(isImageChanging == 2){
index = 1;
//StartCoroutine(ChangeTheImage());
}
else if(isImageChanging == 3){
index = 2;
}
else if(isImageChanging == 4){
index = 3;
}
StartCoroutine(ChangeTheImage());
transform.renderer.material.mainTexture = image1[index];
}
void OnMouseDown() {
Score += 100;
}
void OnCollisionEnter(Collision collision) {
if(collision.gameObject.name == "base"){
Destroy(transform.gameObject);
}
}
IEnumerator ChangeTheImage(){
//Debug.Log("isGoldenEggClicked33 "+isGoldenEggClicked );
if(isImageChanging == 1) {
yield return new WaitForSeconds(0.06f);
isImageChanging = 2;
Debug.Log("isImageChanging " + isImageChanging);
}
else if(isImageChanging == 2) {
yield return new WaitForSeconds(0.06f);
isImageChanging = 3;
Debug.Log("isImageChanging " + isImageChanging);
}
else if(isImageChanging == 3) {
yield return new WaitForSeconds(0.06f);
isImageChanging = 4;
Debug.Log("isImageChanging " + isImageChanging);
}
else if(isImageChanging == 4) {
yield return new WaitForSeconds(0.06f);
isImageChanging = 1;
Debug.Log("isImageChanging " + isImageChanging);
}
//Debug.Log("isGoldenEggClicked " + isGoldenEggClicked);
}
}
Is there any other and gud way to do this??
Try above code. I've made changes to texture as material. create 4 types material in Resources folder and named them as Gready21,Gready22,Gready23,Gready24
– Santosh_Patilthanx santosh but the code you mentioned is not working as I want.
– shadab_badar