change the source image in update function? help?

Hello all,

I have some code which changes the source image on a ui button when it it clicked. I want however for this button’s source image to be changed in the update function once a particular condition is met. How can I do this?
This is what I have tried so far:
I tried adding the code to the button and then adding the update function to the on click () area - that didn’t work.
I tried adding the code to a separate gameobject, referencing the button, and then trying to change the source image in the update function - that too didn’t work.

I am pretty lost as to what can be done to fix this. Can someone save me here?

here is the most recent code :

#pragma strict
var button1 : UnityEngine.UI.Button;
var locked : Sprite;
var select : Sprite;
var done : int;

function Update () {
if (done >= 1 ){
button1.image.overrideSprite = select;
}
else {button1.image.overrideSprite = locked;
}

Why do you want to change it in the Update function? Update is called a lot of times per second, your current code is setting the overrideSprite to “locked” on every frame, and setting it to “select” the frames where “done” is equals or greater than 1. This is a lot or redundant work, but it should change the sprite anyway.

For your current approach, check that “done” keeps it’s value, if you turn it back to 0 at some point the overrideSprite will be changed again to “locked”. If that’s OK make sure there are no errors showing in the console, and that you have the correct reference to the button.

As a suggestion, I’d do this OUTSIDE the Update function, you don’t need to change the sprite on every frame, you need to change it when “done” changes. If you have one place where the “done” variable is updated, move that code after the variable is updated. If you have multiple places where “done” is changed, turn “done” into a property and move the code to the setter, so it’s called any time you set a new vale to it. Look here: http://unity3d.com/es/learn/tutorials/modules/intermediate/scripting/properties