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 aan_comel · Oct 28, 2016 at 09:33 AM · lerpresetboolonclickcolor.lerp

Color.Lerp everytime Onclick

Hello, how can I reset the Lerp in Update to be clear to white color again every time press next button? Thank you.

 public class typeText : MonoBehaviour {
 
     private int counter = 0; 
     public GameObject img;
     private bool loop = false;
     float speed;
    
     void Update() {
         speed += Time.deltaTime;
         if (loop) {
             img.GetComponent<Image> ().color = Color.Lerp (Color.clear, Color.white, speed * 0.4f);   }}

     public void next() {
         loop = true;            
         counter++;       
         img.GetComponent<UnityEngine.UI.Image> ().sprite = Resources.Load<Sprite> (counter);    
     }
 }
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
0
Best Answer

Answer by kleber-swf · Oct 28, 2016 at 01:56 PM

Hi there,

If I understand well, you are trying to create a next button that when you click on it, it loads a sprite from the resources, became transparent and visible again smoothly.

First, please cache your image reference. GetComponent is not to meant to be called every frame. Consider to change the field img to type Image or use the GetComponent in the Start() method to cache the image reference to another field.

The problem is that you are not resetting speedcode> nor loop fields.

 public class TypeText : MonoBehaviour {
 
     private int counter = 0;
     public Image img;
     private bool loop = false;
     float speed;
 
     void Update() {
         if (!loop) return;
         speed += Time.deltaTime;
         var t = speed * 0.4f;
         img.color = Color.Lerp(Color.clear, Color.white, t);
         if (t >= 1f) loop = false;
     }
 
     public void next() {
         loop = true;
         speed = 0;
         counter++;
         img.sprite = Resources.Load<Sprite>(counter);
     }
 }

Another solution could be create an Animation and use it in the Button component.

Comment
Add comment · Show 1 · 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
avatar image aan_comel · Oct 31, 2016 at 01:52 AM 0
Share

Wow! This works. Thank you so much for your help.

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

60 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 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

Lerping 2 values on a shader on button press. 1 Answer

Colour Lerp on 2D point lighting for fire effect. 1 Answer

How do you Lerp light.color into multiple colors? 2 Answers

smoothly switch between 2 colors that are using lerp 2 Answers

Sprite Color Lerp during Length of Sprite Animation 1 Answer


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