Texture Swap Animation question

Hi, I’m looking at this code at the unity wiki for the texture swap animator. Here’s the code

var frames : Texture2D[];

var framesPerSecond = 10.0;

function Update () {
    var index : int = Time.time * framesPerSecond;
    index = index % frames.Length;
    renderer.material.mainTexture = frames[index];
}

How do I code it to execute just once? I tried using if statements, for loops, but nothing seems to work.
Thanks in advance.
-Hakimo

If you want to do it once at the start of the program, you use the
function Start() {
}

If you want to do it once in the update, you would probably use a boolean variable;
var done = false;

function Update(){
if(!done){
//swap code
done = true;
}
}

Thanks very much. I managed to sort out the problem. I used the method you mentioned and it couldn’t work because the script was running in the background and it’s linked to another script activated by a OnTriggerEnter. It’s a group project so it was a bit tedious to change all the codes. So what I did was set the script to be non-active and only to be active when it collides.

Thanks again.

  • Hakimo

Hi Hakimo,

I’m having a very similar problem, and I was wondering if you could explain how you set the script to be non-active, and then set it to be active? Is there any hints or code you can post? Thank you very much!

Thanks!

Hi KeVro,

Sorry for the late reply. Been busy lately that I didn’t manage to check the forums. I’ll try my best to explain what I did:

First I have a gameobject set as a box collider. I added this script which has:

// Your target where you want to have access to
var target : GameObject;

// Use OnTrigger functions
function OnTriggerEnter(hit : Collider)
{
     if (hit.gameObject.tag == "Character")
	   {
	   		target.GetComponent(ScriptAnimatedButtonDoor).enabled = true;
	    }
}

function OnTriggerExit(hit : Collider)
{
     if (hit.gameObject.tag == "Character")
	   {
	   		target.GetComponent(ScriptAnimatedButtonDoor).enabled = false;
	    }
}

I guess the important thing to look at is gameobject.getcomponent.

Hope that helps.