Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by Vaga · Sep 07, 2012 at 03:52 PM · colortimer

How to randomly change a color on an object in C#

Hi Gurus

Can you please assist me, i want to change a color on an object during runtime. I have the code and its working but i have put it in the Update function which changes the color of the object quickly and i was hoping for it to take at least a second or so before changing to anther color. He is the code below:

 // Update is called once per frame

 void Update () {
 
     // pick a random color
     Color newColor = new Color( Random.value, Random.value, Random.value, 1.0f );
 
     // apply it on current object's material
     renderer.material.color = newColor;     
 
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Waffle842 · Oct 18, 2013 at 06:04 PM

 {
     public float timer = 0.0f;
     
     void Start () 
     {
     
     }
     
     
     void Update () 
     {
         timer += Time.deltaTime;
         if (timer >= 2.0f)//change the float value here to change how long it takes to switch.
         {
             // pick a random color
             Color newColor = new Color( Random.value, Random.value, Random.value, 1.0f );
             // apply it on current object's material
             renderer.material.color = newColor;
             timer = 0;
         }
  
         
     }
 }
 
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 Waffle842 · Oct 18, 2013 at 06:16 PM 0
Share

or you could put in a timer. not the most elegant solution...but still.

avatar image
4

Answer by Piflik · Sep 07, 2012 at 03:55 PM

Have a look at InvokeRepeating().

Comment
Add comment · Show 4 · 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 Vaga · Sep 08, 2012 at 02:32 PM -1
Share

Hi Philipp

and where would i put the InvokeRepeating function inside Update?, cause when i put it there its not effective

Regards; V

avatar image Piflik Vaga · Sep 08, 2012 at 02:36 PM 1
Share

Please don't post comments as answers.

You want to call InvokeRepeating() only once, so either put it in Start()/Awake(), if you want it to begin changing the color immediately, or use some other method to trigger it (for example OnCollision) and wrap it into an if statement, so you can prevent it from being called again while it is already running.

avatar image Vaga Vaga · Sep 08, 2012 at 03:06 PM 0
Share

Hi Piflik

I have put it in the Awake method and it working fine...thanks :)

Regards; V

avatar image sdgd Vaga · Oct 18, 2013 at 06:21 PM 0
Share

if you vote down and comment on the answer that shouldn't be an answer and if I come near by I will upvote your comment and convert answer in to a comment

I have downvoted the answer, ...

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

15 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

How do I make my timer start and stop when on certain levels? 1 Answer

Can anyone help me figure this script out? (Light Trigger) 1 Answer

Timer not work :( 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