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);
}
}
}
}
I still don't make it, I wanted try before If this would work
Press F7 in $$anonymous$$onobehavior, what error do you get?
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);
}
}
}
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
on = !on;
Because of that.
use this ins$$anonymous$$d:
on=false;
Ok so It starts at the first input, but If I re-click the button It doesn't stop anymore. o.o
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.
Unfortunately the problem is not solved.. I get always that error.. The i
in IEnumerator ChangeLight() are red... why?
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.
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.
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
Follow this Question
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