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
0
Question by JayFitz91 · Jul 17, 2014 at 08:56 PM · spawnyieldstartcoroutine

spawn an object using yield/startCoroutine

I'm trying to get my head around yield() and startCoroutine().

I currently have an object moving in a direction on screen, I have spawn points set up around the scene and I would like an object to spawn from these points every 5 seconds. (I'm just using one for now as a reference)

The problem is, the console never goes into the functions I have setup for this, I get no console output. Code below:

 using UnityEngine;
 using System.Collections;
 
 public class spawn : MonoBehaviour
 {
     // Prefab to spawn
     public int distance = 10;
     public int timeDelay = 5;
     public float speed = 0.1f;
     public Transform clone;
     public GameObject spawnPoint;
 
     void Update()
     {
         spawnDisc();
         transform.position += transform.forward * distance * speed;
         
     }
 
     IEnumerator spawnDisc()
     {
     //Never gets here
         print("Gotten to spawnDisc");
         yield return StartCoroutine(myDelayMethod(timeDelay));
         Instantiate (clone, spawnPoint.transform.position, spawnPoint.transform.rotation);
         
     }
 
     IEnumerator myDelayMethod(int delay)
     {
     //or here
         yield return new WaitForSeconds (delay);
         print ("Gotten to myDelayMethod");
     }
     
 }

I've only just started using the yield/startCoroutine functions so I'm trying to wrap my head around it but if someone could tell me where I'm going wrong, I'd appreciate it

Comment
Add comment
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

2 Replies

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

Answer by ChrisSch · Jul 17, 2014 at 09:12 PM

Wow wow wow hold up a sec. xD You don't need to call a coroutine twice if you only have one 5 sec delay. Also I don't think you can call a coroutine every frame inside of update, or it will run instantly.

But yours probably isn't working at all because you're calling it like spawnDisc();, I think it should be StartCoroutine("spawnDisc"); and also having two IEnumerators for one coroutine isn't needed.

Here's a simplest version of it.

 IEnumerator SpawnDelay()
 {
     Debug.Log("Happens before");
     yield return new WaitForSeconds(5);
     Debug.Log("Happens after");
 }
 
 void Start()
 {
     StartCoroutine("SpawnDelay");
 }


If I'm right and you can't call a coroutine inside Update every frame, you'll have to use booleans or something to check its only called one frame.

But another way, the one I'm using for spawning atm, is without coroutines.

Goes something like this:

 public float spawnTime = 5;
 private float curSpawnTime;
 
 void Start()
 {
     curSpawnTime = spawnTime;
 }
 
 void Update()
 {
     curSpawnTime-=1 * Time.deltaTime;
     if(curSpawnTime <= 0)
     {
         Spawn();
         curSpawnTime = spawnTime;
     }
 
 }
 
 void Spawn();
 {
     //Your spawning code
 }
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 JayFitz91 · Jul 17, 2014 at 09:18 PM 0
Share

Looking at yours seems so much simpler :P I think I just overwhelmed myself with all of this ha. I'll implement this and see how i get on, cheers Chris

avatar image JayFitz91 · Jul 17, 2014 at 09:22 PM 0
Share

It's clear I haven't a clue about Coroutines ha :P You're way was much simpler, thanks for the help

avatar image ChrisSch · Jul 17, 2014 at 09:24 PM 0
Share

Anytime! :) And don't worry about it, you'll get there. Wait till you start dealing with Quaternions. xD

avatar image
0

Answer by purpl3grape · Jul 27, 2016 at 04:56 PM

Hi, I actually was working on this in my project. So here is my solution. First:

1) requestRespawnPlayer is a bool
2) Paste this and modify where necessary
3) @ Start() implement. Set the last yield to a reasonable interval you want it to repeat by

 IEnumerator PlayerRespawnCoroutine(){
     while (true) {
         if(requestRespawnPlayer){
             yield return new WaitForSeconds (2f);
             SpawnMyPlayer(playerPrefab.name, _teamSelID, classSelID);
             requestRespawnPlayer = false;
         }
         yield return new WaitForSeconds(.2f);
     }
 }
Comment
Add comment · 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

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

23 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

Related Questions

new Thread vs StartCoroutine 2 Answers

Yield position within while loop in coroutine 1 Answer

Yield and WaitForSeconds not stopping at correct points? 1 Answer

Gets stuck on WaitForSeconds 2 Answers

Unity keep crashing with StartCoroutine and yield 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