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 Safforn · Jun 06, 2013 at 11:26 AM · c#lightchangestartcoroutineback

C# change light color every few seconds

Hello,

I have an object and this object has a child(cubeLight). I need to change this light from the color blue to red every few seconds. so it changes to blue and back to red and back to blue etc. my code only changes it once and I have no idea why.

my code: public float resetTime = 0.5f; public Light cubeLight; void Awake() { StartCoroutine("GoBlue"); } IEnumerator GoBlue() { while(true) { cubeLight.color = Color.blue; Debug.Log("blue"); yield return new WaitForSeconds(resetTime); } } }

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by bodec · Jun 06, 2013 at 11:32 AM

in the awake function you ask it to change to blue you never change it to red or anything else from there out.

Comment
Add comment · Show 6 · 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 Safforn · Jun 06, 2013 at 11:49 AM 0
Share

using UnityEngine; using System.Collections; public class Switch : $$anonymous$$onoBehaviour { public float resetTime = 0.5f; public Light cubeLight; void Awake() { StartCoroutine("GoBlue"); cubeLight.color = Color.blue; } IEnumerator GoBlue() { while(true) { yield return new WaitForSeconds(resetTime); } } }

won't work either :(

avatar image bodec · Jun 06, 2013 at 01:29 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class lightchange : $$anonymous$$onoBehaviour {
     public bool lightChange;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(lightChange){
             light.color = Color.red;
         }else{
             light.color = Color.blue;
         }
             
     }
     
 }
 
 
 this will change the light based on a bool change short and simple just need to send a message from the trigger to the bool
avatar image bodec · Jun 06, 2013 at 01:31 PM 0
Share

if you want it to go non stop set up timers that change it back and forth and set a bool to turn on a function to start the timers ticking

avatar image Safforn · Jun 06, 2013 at 01:34 PM 0
Share

what seems to work also is this

 `IEnumerator GoBlue()
     {
         while(true)
         {
         cubeLight.color = Color.blue;
         yield return new WaitForSeconds(resetTime);
         cubeLight.color = Color.red;
         yield return new WaitForSeconds(resetTime);
         }
     
     }`

it works good enough :P

avatar image bodec · Jun 06, 2013 at 01:38 PM 0
Share

Glad you figured it out. sorry I couldn't help more.

Show more comments
avatar image
1

Answer by fafase · Jun 06, 2013 at 02:15 PM

Two ways, either you use the update or you use a coroutine that never ends. The rest is just a timer.

In Update:

 var timer:float;
 Color[] color = new Color[2];
 int index = 0;
 function Start(){
     color[0] = Color.blue;
     color[1] = Color.red;
     cubeCoor = color[index];
 }
 
 function Update(){
     timer += Time.deltaTime;
     if(timer > 2){
         if(++index == color.Length)index = 0;
         cubeColor = color[index];
         timer = 0;
     }
 } 

Coroutine:

 function CoroutineChanger(){
    var timer :float;
    while(true){
       while(timer < 2){
          yield;
       }
       if(++index == color.Length)index = 0;
       cubeColor = color[index];
       timer = 0;
    }
 }

Same, same but different.

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
avatar image
0

Answer by Emihaa · Aug 01, 2013 at 06:50 AM

Put this script to the object where the material is that you want to change color. Then put on the colorA and colorB the colors you want it to change. If you want to change the speed you can do something like "public float speed = 1.0F; " i guess. colorMaterial needs to be the name of the material you want to use. Also the "_Color" might be needed to change depending what the color is named on you shader.

using UnityEngine; using System.Collections;

 public class ChangeColor : MonoBehaviour {
 
     public Color colorA;
     public Color colorB;
     public float speed;
     public Material colorMaterial;
 
     void Update () {
         Color color = Color.Lerp(colorA, colorB, Mathf.PingPong(Time.time * speed, 1));
         colorMaterial.SetColor("_Color", color);
     }
 }
 
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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is it possible to change keyboard input inGame 1 Answer

How do I initialize a simple light in C#? 2 Answers

How to stop a co-routine in C# instantly? 5 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