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 XeroKira · Feb 06, 2019 at 03:43 AM · coroutinelerpupdate problemfor loopcolor.lerp

If statement not being fullfilled until GameObject inspected in inspector.

I have a bit of code that alternates the target color of a Color.lerp if that Color.Lerp reaches it's target color. I have it working on some GameObjects, but for others, it gets to first target color, and will only update and begin to continously switch when I inspect it in the hierarchy. Any advice?

     public IEnumerator RainbowIdle()
     {
         for (int i = 0; i < gameObject.transform.childCount; i++)
         {
             transform.GetChild(i).gameObject.GetComponent<SpriteRenderer>().color = Color.Lerp(transform.GetChild(i).gameObject.GetComponent<SpriteRenderer>().color, rainbow[currentColor], Time.deltaTime / idleLength);
         }
         if (transform.GetChild(0).gameObject.GetComponent<SpriteRenderer>().color == rainbow[currentColor])
         {
             if (currentColor == rainbow.Length - 1)
             {
                 currentColor = 0;
             }
             else
             {
                 currentColor += 1;
             }
         }
         yield return null;
         StartCoroutine(RainbowIdle());
     }

Comment
Add comment · Show 1
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 Bonfire-Boy · Feb 06, 2019 at 12:56 PM 0
Share

There's nothing in what you've shown us that looks like it'll be dependent on whether or not something is currently selected in the editor. That's more likely to be to do with how you're starting this process (which you haven't shown us).

Having said that, the code as stands is very confusing. The fact that your coroutine only runs for one frame and then restarts itself is frankly a bit odd. And specifically 1) you're always lerping between the current colour and the target colour (so you don't have a linear change) 2) your end point relies on floating point equality which is dangerous (and may be related to why your code isn't moving on, if that's the if-statement you're referring to): if you want a process to take a particular amount of time, it's much better to use that as your endpoint (and with an inequality test rather than equality).

Your code is also very bad performance-wise as you are doing lots of unnecessary calls to GetComponent and Lerp. I couldn't bring myself to use it as a starting point... use variables and functions!

I would suggest that something along these lines would make more sense. Note that I'm not making any function calls in the block of code that gets called every frame during the lerping (ie the while loop), beyond those that absolutely need to be called.

 public float changeColourDuration;
 public Color[] rainbow;

 SpriteRenderer[] rendArray;
 int currentColour = 0;
 
 Start()
 {
     rendArray = transform.GetComponentsInChildren<SpriteRenderer>();
     SetAllColoursTo(0);    
     StartCoroutine( LerpAllColoursTo( 1);
 }
 
 void SetAllColoursTo(int i)
 {
     for (int i = 0; i < rendArray.Length; i++)
     {
         rendArray[i].color = rainbow[i];
     }
 }
 
 IEnumerator LerpAllColoursTo(int i)
 {
     Color startColour = rendArray[currentColour].color;
     Color endColour = rendArray[i].color;
 
     float elapsed = 0f;
     while (elapsed < changeColourDuration)
     {
          elapsed += Time.deltaTime;
          SetAllColoursTo( Color.Lerp( startColour, endColour, elapsed/changeColourDuration);
          yield return null;
     }
     currentColour = i;
     StartCoroutine( LerpAllColoursTo( (i+1) % rainbow.Length );
 }
 

0 Replies

· Add your reply
  • Sort: 

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

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

lerp transparency back and forth, while cycling through rainbow? 0 Answers

Lerp to position, after a while come back, problem occured using Coroutines 1 Answer

Lerp a light intensity gradually to 0 1 Answer

restrict Lerp to only one axis? 1 Answer

Lerping multiple materials of a gameobject within a coroutine 0 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