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 /
  • Help Room /
avatar image
0
Question by Lore_NN · Nov 04, 2015 at 01:03 PM · c#scripting problemerrorienumeratorsymbol

IEnumerator, Unexpected symbol?

Hi, It's since a week that I've this problem, in IEnumerator function, i get the error "Unexpected symbol". I really hope that someone can Help me! :)

  using System;
      using UnityEngine;
      
      public class LightAnimator : MonoBehaviour {
      
          Coroutine lightOnCoroutine;
          Coroutine lightOffCoroutine;
      
          public Light[] lights;
          public KeyCode controlKey = KeyCode.X;
          private bool on = false;
          
          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);
                  }
              }
              
          }
  }

 
Comment
Add comment · Show 4
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 Landern · Nov 05, 2015 at 03:46 PM 0
Share

Where is the method called StopLight?

avatar image Lore_NN Landern · Nov 05, 2015 at 04:23 PM 0
Share

I still don't make it, I wanted try before If this would work

avatar image OctoMan Lore_NN · Nov 05, 2015 at 04:41 PM 0
Share

Press F7 in $$anonymous$$onobehavior, what error do you get?

Show more comments

3 Replies

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

Answer by OctoMan · Nov 05, 2015 at 06:41 PM

Fixed several things, should work now! Just add your other coroutine now!

 using UnityEngine;
 using System.Collections;
 
 public class LightAnimator : MonoBehaviour {
     
     Coroutine lightOnCoroutine;
     Coroutine lightOffCoroutine;
     
     public Light[] lights;
     public KeyCode controlKey = KeyCode.X;
     private bool on = false;
     
     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 new WaitForSeconds(1);
         }
     }
 }


Comment
Add comment · Show 3 · 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 Lore_NN · Nov 05, 2015 at 07:16 PM 0
Share

Finally It works! Thank you.. the last question, I've made also the StopLight, It works well,. but I don't understand why for make start the script I have to press two times the key.. Then is enought one time.. but When i press play in Unity, for make start the light I have to press two times the key

avatar image OctoMan Lore_NN · Nov 05, 2015 at 07:41 PM 0
Share
 on = !on;

Because of that.

use this ins$$anonymous$$d:

 on=false;

avatar image Lore_NN OctoMan · Nov 05, 2015 at 09:39 PM 0
Share

Ok so It starts at the first input, but If I re-click the button It doesn't stop anymore. o.o

avatar image
0

Answer by Dave-Carlile · Nov 04, 2015 at 01:05 PM

Line 37 needs to read:

 yield return new WaitForSeconds(1);

WaitForSeconds is a class, so what you're really doing here is creating an instance of the class and returning it as the function result. In order to create a new class you must use the new keyword.

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 Lore_NN · Nov 04, 2015 at 09:08 PM 0
Share

Unfortunately the problem is not solved.. I get always that error.. The i in IEnumerator ChangeLight() are red... why?

avatar image Dave-Carlile Lore_NN · Nov 04, 2015 at 09:32 PM 0
Share

Hard to say without seeing all of the code - don't see where lights is declared or anything. I would suggest this: Indent your code properly so all of the parentheses line up, and lines in the same block all start in the same column. Then edit your question and paste the new code - all of it, along with the exact error message including the line number information.

You may not know this, but if you double click on the error message it should take you to the offending line in the editor. When there's a missing brace or something it sometimes isn't able to pinpoint that though since it would only notice a problem on the final missing brace. So you'd look backwards from there.

avatar image
0

Answer by NoseKills · Nov 05, 2015 at 05:18 PM

Isn't your IEnumerator method declared inside Update? Declare methods inside a class but not inside other methods. Move it out of Update.

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 Lore_NN · Nov 05, 2015 at 06:31 PM 0
Share

I tried, seems It work for what concern the brackets, but now there is this error: The type or namespace name `IEnumerator' could not be found. Are you missing a using directive or an assembly reference? I have no idea for what could be :/

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

6 People are following this question.

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

Related Questions

Instatiated object not being referenced in Start function? 2 Answers

Help! Values from previous Serialized List<> shows up again after a while using Coroutine 0 Answers

Сonstructor return null 0 Answers

[HELP] Following the 2D character controller tutorial from the live training section (C#) 0 Answers

tengo un problema con este script no puedo hacer que salte el personaje en un 2f,hi i have a problem making my chacter jump with this script 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