Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

How to change RawImage texture to another one when i press a buton for it 0 Answers

Issues with Update and script. Why is it not repeating? 1 Answer

Cursor appearance change when mouse over a GUI button 2 Answers

How to make a distance counter that updates? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges