• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mealone · Oct 13, 2015 at 05:56 PM · scripting problembuttonupdatechangecodepage

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DiegoSLTS · Oct 13, 2015 at 08:49 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Move camera upon button press 0 Answers

Cursor appearance change when mouse over a GUI button 2 Answers

Update mesh collider after changing vertices at run time 0 Answers

How to get an array of all Buttons attached to a Panel? 2 Answers

Making button activate update with boolean 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges