Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Abhi94 · May 04, 2015 at 11:25 AM · update function

How can i stop my update function after showing 5 random colors ?

i need my update function to slowly update my color ( 1 second - 1 color) using UnityEngine; using Unity.Engine.UI;

 public class ColorChange : MonoBehaviour {
 
  void Update() {
 
 GetComponenet<Image>().color = new Color(Random.Range(0f, 1f),Random.Range(0f, 1f), Random.Range(0f, 1f));
 
 }
 }
Comment
Add comment · Show 6
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 KalleH · May 04, 2015 at 11:35 AM 0
Share

Hey! Have a look at InvokeRepeating and CancelInvoke or coroutines. You can just have a variable storing the amount of times the color has been shown and once it reaches 5, stop changing it.

avatar image Abhi94 · May 04, 2015 at 05:09 PM 0
Share

this is stoping after 5 seconds , but I also want it to only show 5 colors in 5 seconds not more than that -:

using UnityEngine; using System.Collections; using UnityEngine.UI; public class ColorChange : $$anonymous$$onoBehaviour {

     float delay = 0;

 void Update()
 {


     if (delay >= 5f) {
         return;
     
     } else{
     
         GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));

         delay += Time.deltaTime;
     }
 }

}

avatar image JoelSeidel · May 04, 2015 at 05:11 PM 0
Share

You can't stop the update function. It doesn't work that way. The Unity Engine will call the update function ever frame no matter what. Just use an accumulator and make that PART of the the update function test to false so it does not execute.

avatar image Sondre-S · May 04, 2015 at 05:32 PM 0
Share

If I understood this correctly you want to change the color of an image each second for 5 seconds? In the same way as a traffic light, but with 5 colors and randomly?

avatar image Ilgiz · May 04, 2015 at 10:50 PM 1
Share
 using UnityEngine; using System.Collections; using UnityEngine.UI; public class ColorChange : $$anonymous$$onoBehaviour {
 
      float delay = 0;
  float timeLeft = 0;
  void Update()
  {
  timeLeft+= Time.deltaTime;
  
      if (delay >= 5f) {
          return;
      
      } else{
       if (timeLeft >= 1f) {
          GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
 timeLeft=0;
  }
          delay += Time.deltaTime;
      }
  }
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Villaindro · May 05, 2015 at 01:05 AM

What about something like this:

     // increase this to slow down color changed
     float smooth = 0.05f; 
     int duration = 5;
 
 
 
     // Use this for initialization
     void Start()
     {
         StartCoroutine(FadeOut(5f));
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     private IEnumerator FadeOut(float time)
     {
 
         float progress = 0;
         float increment = smooth / time;
 
         while (progress < 1)
         {
             Color colorToLerp = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
             Color startColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
             gameObject.GetComponent<Image>().color = Color.Lerp(startColor, colorToLerp, progress);
             progress += increment;
             yield return new WaitForSeconds(smooth);
         }
     }
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 Abhi94 · May 05, 2015 at 10:28 AM 0
Share

Villaindro thanks , but it doesn't seem to work

avatar image
0

Answer by Sondre-S · May 05, 2015 at 10:31 AM

Here's the code I use for my traffic light(you can just rename the colors to color1, color2 etc., add a forth and fifth color and set all colors to 1 sec). I don't know if you want to make sure it shows a different color each time, but if you do you'll have to store the colors already shown in some variables and make sure it doesnt show that color. Also this script is probably unneccesarily complicated, but it works:

  var Seconds:float= 0.0169999f; 
     var Red:float= 15f; 
     var Yellow:float= 3f;
     var Green:float = 15f;
     var Green:float = 15f;
     
     var TrafTimer:float;

     var changeColors: boolean;
 
    function Start() {}
     
     function Update () {

     if(changeColors){
     
     TrafTimer += Seconds;
     
     ///// light is red ////////
     if(TrafTimer > 0 && TrafTimer < Red) 
     {
     // you can set a random color here
     }
     /////// light is yellow /////////
     if(TrafTimer > Red && TrafTimer < (Red+Yellow))  
     {
     // you can set a random color here 
     }
     //////// light is green ////////
     if(TrafTimer > (Red+Yellow) && TrafTimer < (Red+Yellow+Green)) 
     {
     // you can set a random color here 
     }
 
     // light is yellow ///////////
     if(TrafTimer > (Red+Yellow+Green)) 
     {
     // you can set a random color here 
                 
     }
     
     // Reset light back to red and/or just stop changing colors
     if(TrafTimer >= Red+Yellow+Green+Yellow) 
     {
     //TrafTimer=0; 
     changeColors= false; // don't run this anymore
     }    

   }


 
 }
 
 
   
 
 
Comment
Add comment · Show 7 · 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 Abhi94 · May 05, 2015 at 11:58 AM 0
Share

thanks Sondre but it doesn't seem to work it only shows one color. but when I click on checkbox of changeColors then it changes color. please tell me what is wrong using UnityEngine; using System.Collections; using UnityEngine.UI; public class buttonColor : $$anonymous$$onoBehaviour {

 //Here's the code I use for my traffic light(you can just rename the colors to color1, color2 etc., add a forth and fifth color and set all colors to 1 sec), also this script is probably unneccesary complicated, but it works:
 public float Seconds = 1f; 
 public float color1 = 1f; 
 public float color2 = 1f;
 public float color3 = 1f;
 public float color4 = 1f;
 public float color5 = 1f;
 private float TrafTimer;
 public  bool changeColors ;
 
 
 void Start() {}
 void Update () {
     if (changeColors) {
         
         TrafTimer += Seconds;

     
     
         ///// light is red ////////
         if (TrafTimer > 1 && TrafTimer < color1) {
             GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
         }
         /////// light is yellow /////////
         if (TrafTimer > color1 && TrafTimer < (color1 + color2)) {
             GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
         }
         //////// light is green ////////
         if (TrafTimer > (color1 + color2) && TrafTimer < (color1 + color2 + color3)) {
             GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
         }
     
         // light is yellow ///////////
         if (TrafTimer > (color1 + color2 + color3 + color4)) {
             GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
         
         }
     
         // Reset
         if (TrafTimer >= color1 + color2 + color3 + color4 + color5) {
             TrafTimer = 0;
             changeColors = false;
         }    
     
     }
     
 }
avatar image Abhi94 · May 05, 2015 at 12:04 PM 0
Share

thanks "Ilgiz" . it is now showing 5 colors in 5 seconds . And if you could help me with one more thing - if there any way to make the all 5 colors of different shade. ( right now they are quite dull and same)

avatar image Sondre-S · May 06, 2015 at 10:12 AM 0
Share

Guess you figured it out, but you have to set "change colors" to true in the declaration or in function Start().

avatar image Abhi94 · May 06, 2015 at 10:29 AM 0
Share

thanks Sondre , will keep it in $$anonymous$$d

avatar image Ilgiz · May 06, 2015 at 01:48 PM 0
Share

you will need to Lerp between a few color (red -> yellow -> blue -> red) to do a rainbow transition.

try something like it

     Color.Lerp(Color.red, Color.yellow, Random.Range (0f, 1f))
     Color.Lerp(Color.yellow , Color.blue , Random.Range (0f, 1f))
     Color.Lerp(Color.blue, Color.red, Random.Range (0f, 1f))
Show more comments

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

7 People are following this question.

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

Related Questions

Instantiating an object only once within Update() 1 Answer

UI Image CrossFadeAlpha IEnumerator 2 Answers

How Much code can be put in a start function? 1 Answer

how to let user only have one go at moving? 1 Answer

Testing private/protected methods that use Input System 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