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
1
Question by byurocks23 · Feb 27, 2014 at 04:17 AM · javascripterror messageyieldyield waitforseconds

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();
Comment
Add comment · Show 2
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 nesis · Feb 27, 2014 at 04:23 AM 0
Share

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.

avatar image byurocks23 · Feb 27, 2014 at 04:46 AM 0
Share

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

2 Replies

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

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.)

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 byurocks23 · Feb 27, 2014 at 05:41 AM 0
Share

That's the problem, I am new to Unity and have no idea what that means.

avatar image Eric5h5 · Feb 27, 2014 at 05:45 AM 1
Share
 function EnemyFunction () : IEnumerator {
avatar image whydoidoit · Feb 27, 2014 at 05:46 AM 0
Share

@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?

avatar image Eric5h5 · Feb 27, 2014 at 05:51 AM 0
Share

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. ;)

avatar image byurocks23 · Feb 27, 2014 at 06:07 AM 0
Share

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?

Show more comments
avatar image
1

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;         
        }
     } 
 }   
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 byurocks23 · Feb 27, 2014 at 05:44 AM 0
Share

This worked great.

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

24 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

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


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