- Home /
Function with itself called inside it. (JavaScript)
So i am coding a complex switch statement that basically chooses a vehicle type, then chooses the lane that it will spawn in. At the moment I am currently just getting the structure of my function and that prints what is supposed to happen. I am new to unity so there might be other errors, they aren't part of this question, but I would still appreciate if you would point them out to me.
In my code, after the vehicle is spawned at certain lane(represented by what is printed to the console) It should wait a certain amount of seconds(I just chose three for now) then I want it to call the same function to spawn another vehicle. Except I get the error message:
Assets/Scripts/MasterScript.js(16,41): BCE0070: Definition of 'MasterScript.enemyFunction()' depends on 'MasterScript.enemyFunction()'whose type could not be resolved because of a cycle. Explicitly declare the type of either one to break the cycle.
My code is
function enemyFunction(){
var lane: int = Random.Range(1,4);
var vehicleType: int = Random.Range(1,7);
switch (vehicleType){
case 1: //Semi
case 2:
switch (lane){
case 1:
Debug.Log("Semi, lane 1");
yield WaitForSeconds (3);
enemyFunction();
break;
case 2:
Debug.Log("Semi, lane 2");
yield WaitForSeconds (3);
enemyFunction();
break;
case 3:
Debug.Log("Semi, lane 3");
yield WaitForSeconds (3);
enemyFunction();
break;
}
break;
case 3:
switch (lane){
case 1:
Debug.Log("Bike, lane 1");
yield WaitForSeconds (3);
enemyFunction();
break;
case 2:
Debug.Log("Bike, lane 2");
yield WaitForSeconds (3);
enemyFunction();
break;
case 3:
Debug.Log("Bike, lane 3");
yield WaitForSeconds (3);
enemyFunction();
break;
}
break;
case 4:
case 5:
case 6:
switch (lane){
case 1:
Debug.Log("Car, lane 1");
yield WaitForSeconds (3);
enemyFunction();
break;
case 2:
Debug.Log("Car, lane 2");
yield WaitForSeconds (3);
enemyFunction();
break;
case 3:
Debug.Log("Car, lane 3");
yield WaitForSeconds (3);
enemyFunction();
break;
}
break;
}
}
enemyFunction();
You're using yield, so that means the function needs to be a coroutine. When you want enemyFunction() to get called, use StartCoroutine(enemyFunction()) ins$$anonymous$$d of just enemyFunction() and it should be fine, I think.
So after the yield WaitForSeconds (3);, replace that with StartCoroutine(enemyFunction()),? because I am still getting the same exact error after I do that. In fact, it gave me extra errors
Answer by Eric5h5 · Feb 27, 2014 at 05:37 AM
To literally answer the question, do what the error says and declare the type of the function. (Which is always IEnumerator for coroutines.)
That's the problem, I am new to Unity and have no idea what that means.
@Eric5h5 - Good point :) Will that cause the outer enumerators to never end? Or will they end because Unity Script will convert the recursion call into a StartCoroutine rather than a yield?
From someone's comment about something similar, I think it's not technically recursion since it uses StartCoroutine. But don't hold me to that. ;)
This worked. Would it make a difference in performance between using a function in a function like how I originally had it rather than a while loop as @whydoidoit posted above. Which one would be more on the dot with the timer, especially when multiple enemies are being created quickly one right after another?
Answer by whydoidoit · Feb 27, 2014 at 05:29 AM
What you actually want is a while or for loop like this:
function enemyFunction(){
while(true) {
var lane: int = Random.Range(1,4);
var vehicleType: int = Random.Range(1,7);
switch (vehicleType){
case 1: //Semi
case 2:
switch (lane){
case 1:
Debug.Log("Semi, lane 1");
yield WaitForSeconds (3);
break;
case 2:
Debug.Log("Semi, lane 2");
yield WaitForSeconds (3);
break;
case 3:
Debug.Log("Semi, lane 3");
yield WaitForSeconds (3);
break;
}
break;
case 3:
switch (lane){
case 1:
Debug.Log("Bike, lane 1");
yield WaitForSeconds (3);
break;
case 2:
Debug.Log("Bike, lane 2");
yield WaitForSeconds (3);
break;
case 3:
Debug.Log("Bike, lane 3");
yield WaitForSeconds (3);
break;
}
break;
case 4:
case 5:
case 6:
switch (lane){
case 1:
Debug.Log("Car, lane 1");
yield WaitForSeconds (3);
break;
case 2:
Debug.Log("Car, lane 2");
yield WaitForSeconds (3);
break;
case 3:
Debug.Log("Car, lane 3");
yield WaitForSeconds (3);
break;
}
break;
}
}
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
yield WaitForSeconds(5); IS WAITING FOR EVER 1 Answer
Problem with yield WaitForSeconds (I think) -javascript) 1 Answer
Can't edit built-in array through script unless viewed in Inspector 1 Answer