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 /
This question was closed Nov 07, 2015 at 07:54 PM by PaxStyle for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by PaxStyle · Nov 03, 2015 at 10:31 AM · c#scripting problemcoroutinelight

Light problem

Hi to all.. I have a problem in the void Update, when I press the key the sequence doesn't start, I have to press the key two times.. And then the sequence doesn't stop when I press the key again.. I think I made a mistake in IEnumerator StopLight() Hope someone can Help me to solve my probem :)

 public class LightAnimator : MonoBehaviour {
     // number of seconds between changing light
     public float lightTime = 1;
     // the current light that is lit.
     int currentLight = 0;
     
     public Light[] lights;
     public KeyCode controlKey = KeyCode.X;
     private bool on = false;
     
 
     IEnumerator ChangeLight()
     {
         
         yield return new WaitForSeconds(lightTime);   
         
         Debug.Log("Changing light to number "+currentLight);
         
         // disable the current light
         lights[currentLight].enabled = false;
         
         // change the index to the next light
         currentLight++;
         
         
         
         // if the index is 4 or more, go back to the first light
         if (currentLight >= 4)
             currentLight = 0;
         
         // the current light is now the next light, so enable it.
         lights[currentLight].enabled = true;
         
         // call this method again to create an infinite loop.
         StartCoroutine("ChangeLight");
         
     }
 
     IEnumerator StopLight()
     {
         yield return new WaitForSeconds(lightTime);   
         // disable the current light
         lights[currentLight].enabled = false;
         
         // call this method again to create an infinite loop.
         StartCoroutine("StopLight");
 
     }
 
 
     void Update(){
         
         if (Input.GetKeyDown (controlKey)) {
             on = !on;
 
         if (!on) {
         StartCoroutine ("ChangeLight");  
         }   
         if (on) {
         StartCoroutine ("StopLight");
 
             }
     }
     
 }
 }

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

  • Sort: 
avatar image
0
Best Answer

Answer by rageingnonsense · Nov 03, 2015 at 06:03 PM

do this instead:

 public class LightAnimator : MonoBehaviour {
     Coroutine lightOnCoroutine;
     Coroutine lightOffCoroutine;
 
     void Start() {      
     }
 
     void Update() {
         if (Input.GetKeyDown (controlKey)) {
              on = !on;
  
          if (!on) {
              lightOnCoroutine = StartCoroutine ("ChangeLight");  
             if(lightOffCoroutine != null) {
                   StopCoroutine(lightOffCoroutine);
             }
          }   
          if (on) {
               lightOffCoroutine = StartCoroutine ("StopLight");
               if(lightOnCoroutine != null) {
                     StopCoroutine(lightOnCoroutine);
              }
 
  
              }
      }
 
      IEnumerator ChangeLight() {
           int i = 0;
           while(true) {
                   lights[i].enabled = false;                  
                   i++;
                   if(i > lights.length - 1) {
                          i = 0;
                   }
                  lights[i].enabled = true;
                   yield return WaitForSeconds(1);
           }
      }
 
  }

This is a rough example. The idea is to keep track of your coroutines, and do not rely on infinite recursion for them. You'll have more control.

A better way to do this though is with a single coroutine that checks to see if a flag is set for swapping lights (instead of while(true), do while(myBool)). When you want to switch them off, just set the boolean to false, and manually turn off all the lights (no need for a coroutine to turn them off it seems).

Comment
Add comment · Show 2 · 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 PaxStyle · Nov 03, 2015 at 07:09 PM 0
Share

Thank you for your support! I get this error: Unexpected symbol (', expecting )', ,', ;', [', or =' inside the IEnumerator ChangeLight(), so I cannot try the script :)

avatar image rageingnonsense PaxStyle · Nov 03, 2015 at 11:15 PM 0
Share

As I said, it is REALLY rough. You can't just copy and paste it, you need to use it as an example and apply it. It is probably riddled with syntax errors. Just work through them.

Follow this Question

Answers Answers and Comments

34 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

Related Questions

C# | How to delay a method with parameters 2 Answers

Can't call of type IEnumerable with delegates? 1 Answer

the player is not shooting 1 Answer

how do i use component gathered from ontriggerEnter, outside of that function? 2 Answers

Multiple Cars not working 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