Log in or register to post comments

Want to change the texture of the cube every 3 sec

May 20, 2011 - 10:42am #1

Hi
I have been trying to figure out how to change the texture of the cube dymically
this is the script that i have used. It changes the texture only once

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
public float updateInterval = 0.5F;

private float accum = 0; // FPS accumulated over the interval
private int frames = 0; // Frames drawn over the interval
private float timeleft;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

timeleft -= Time.deltaTime;
accum += Time.timeScale/Time.deltaTime;
++frames;

// Interval ended - update GUI text and start new interval
if( timeleft {
// display two fractional digits (f2 format)
float fps = accum/frames;
string format = System.String.Format("{0:F2} FPS",fps);
guiText.text = format;

if(fps {
Texture2D tex = Resources.Load("5") as Texture2D;
GameObject.Find("Cube").renderer.material.mainTexture= tex;
}
else
{

Texture2D tex = Resources.Load("1") as Texture2D;
GameObject.Find("Cube").renderer.material.mainTexture= tex;
}
// DebugConsole.Log(format,level);
timeleft = updateInterval;
accum = 0.0F;
frames = 0;
}

}
}

I think update function is been called only once

Re: Want to change the texture of the cube every 3 sec

May 20, 2011 - 11:27am #3

Ah, I just read the title of your post again. Your code won't change the texture every three seconds, instead it changes the texture when the framerate changes. Try reworking the code a bit, if you use updateInterval = 3.0f you should be able to toggle the texture directly in your if statement (don't check against the fps).

- Kim

Re: Want to change the texture of the cube every 3 sec

May 20, 2011 - 11:22am #2

Update() is called each frame, trying adding a print() statement at the start of it and watch the runtime log. You can view the log file using the DDMS tool that comes with Android.

Are you sure that the framerate is hovering around 30 fps? Watch the framerate and see if your texture changes when it drops above or below that value. Also make sure there aren't any errors in the log.

- Kim

Log in or register to post comments