Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by soviet_pig · Sep 16, 2016 at 12:38 PM · 2dinstantiateyield waitforsecondsrandomspawning

Trying to spawn random falling rocks, can't figure out why it's not working. Help would be appreciated!

I am making a 2D adventure game. In one level, the player is faced with a chamber full of rocks that are falling from above screen height and down to the ground the player is on, and kills the player if one hits him.

The problem I am having is the delay between rock spawns. I tried an IEnumerator function called after every rock spawn, but it doesn't seem to work. Unity just freezes when I run the code.

Here is my full code:

 using UnityEngine;
 using System.Collections;
 
 public class SpawnRocks : MonoBehaviour {
 
     public static Transform rockSpawn;
     public GameObject[] rocks;
 
     int spawnSpot;
 
     void Start () {
         SpawnRock();
     }
     
     void Update () {
 
     }
 
     void SpawnRock() {
         //change up spawn position of rocks a bit
         spawnSpot = Random.Range(-1, 8);
         //pick a random rock prefab from our array of prefabs, copy it to the new theRock object
         GameObject theRock = rocks[Random.Range(0, rocks.Length)];
 
         //spawn theRock
         Instantiate(theRock, new Vector3(transform.position.x + spawnSpot, transform.position.y, transform.position.z), Quaternion.identity);
         //give it a random direction and velocity
         theRock.GetComponent<Rigidbody2D>().velocity = ((Vector2)Random.onUnitSphere).normalized * Random.Range(5, 10);
 
         //destroy theRock after 2 seconds
         Destroy(theRock, 2f);
 
         //wait for a random amount of seconds
         Wait();
 
         //spawn another rock
         SpawnRock();
     }
 
     public static IEnumerator Wait() {
          yield return new WaitForSeconds(Random.Range(.2f,.5f));
     }
 }

If I delete the recursive SpawnRock(); call at the bottom, and take out SpawnRock(); from the Start function, then add it to just Update(), rocks just continually spawn until the game lags out. I know it must be something wrong with my Wait function, but I cannot find out what.

Any help is much appreciated! Thank you in advance.

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
1

Answer by Landern · Sep 16, 2016 at 12:41 PM

You should use StartCoroutine in combination with WaitForSeconds to create a continuation for x seconds, currently as you stated it will just keep going over and over. It's better to use the Update/FixedUpdate methods to execute these functions, but either way, StartCoroutine is your friend. Optionally InvokeRepeating

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
avatar image
0

Answer by NerdClown · Sep 16, 2016 at 12:45 PM

I think the problem is here:

          //wait for a random amount of seconds
          Wait();

This does not actually wait for a number of second before moving to the next instruction. You can use it to get a similar behavior with coroutines.

Here's an example of recursion made using coroutines in Unity.

And here is some unity documentation for the coroutine thing, with a bunch of examples in there.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Character Shooting Mechanic 2D Issues 0 Answers

Why is it that some sprites render in game view and others do not? 1 Answer

Projectile Instantiating at opposite position 0 Answers

How do I bring an instantiated projectile back towards the player if the player tells it to? Similar to a boomerang. 0 Answers

Spawning random tiles 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