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 /
This question was closed Sep 15, 2014 at 10:25 AM by galaboy for the following reason:

The question is answered, right answer was accepted

avatar image
-1
Question by galaboy · Jul 29, 2014 at 03:26 AM · ienumerator

trying to run a co routine function gets error

am spawning an object at some random time interval. am using co-routines for that. it shows a error message which i couldn't understand. can some one help me what is the error about. thanks in advance.

 using UnityEngine;
 using System.Collections;
 
 public class enemySpawner_5 : MonoBehaviour {
 
     public GameObject[] enemies_5;
     bool isSpawning = false;
     public int minTime = 2;
     public int maxTime = 4;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         //We only want to spawn one at a time, so make sure we're not already making that call
         if(!isSpawning)
         {
             //making a condition that we can spawn
             isSpawning = true;
 
             int enemyIndex = Random.Range(0, enemies_5.Length);
             StartCoroutine(spawnObject(enemyIndex, Random.Range(minTime, maxTime)));
 
         }
     
     }
 
     IEnumerator spawnObject( int index, int seconds)
     {
         //instantiating position
         Vector3 position = new Vector3(-1.355466f, 7f, -0.5f);
 
         //instantiating the object
         Instantiate(enemies_5[index], position, transform.rotation);
 
         //we have spawned so we could start another spawn
         isSpawning = false;
     }
 }
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 nockieboy · Jul 29, 2014 at 03:56 AM 0
Share

Erm... what's the error you're getting?

avatar image Stormizin · Jul 29, 2014 at 03:57 AM 0
Share

which error message did you getting?

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by sethuraj · Jul 29, 2014 at 04:08 AM

The problem here is that your Coroutine is not returning/yielding any values here.Try like this

 using UnityEngine;
 using System.Collections;
  
 public class enemySpawner_5 : MonoBehaviour 
 { 
     public GameObject[] enemies_5;
     bool isSpawning = false;
     public int minTime = 2;
     public int maxTime = 4;
  
     // Use this for initialization
     void Start () 
     {
  
     }
  
     // Update is called once per frame
     void Update ()
     { 
         //We only want to spawn one at a time, so make sure we're not already making that call
         if(!isSpawning)
         { 
             int enemyIndex = Random.Range(0, enemies_5.Length);
             StartCoroutine(spawnObject(enemyIndex, Random.Range(minTime, maxTime))); 
 
             //Spawn only once
             isSpawning = true;
         } 
     }
  
     IEnumerator spawnObject( int index, int seconds)
     {
         //instantiating position
         Vector3 position = new Vector3(-1.355466f, 7f, -0.5f);
  
         //instantiating the object
         Instantiate(enemies_5[index], position, transform.rotation);
 
         //Wait for the random seconds
         yield return new WaitForSeconds(seconds);
  
         //we have spawned so we could start another spawn
         isSpawning = false;
     }
 }

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 galaboy · Jul 30, 2014 at 04:15 AM 1
Share

thanks for the reply, it was my mistake. as i was at office, in a hurry to leave i forgot to post the error. but thanks for the post. that was a silly mistake. its working good.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

error CS0161: not all code paths return a value 2 Answers

yield return M() vs yield return StartCoroutine 2 Answers

Returning an IEnumerator as an int? 1 Answer

Waiting twice inside coroutine (C#) 2 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