Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 tandtgardensolutions · Jan 31, 2019 at 06:09 PM · objectobject pool

Help with Object Pool Spawning.

Hi Everybody,

I'm just taking my first steps into unity and c# after focusing on Python for the past year or so, mostly by following the Unity tutorials. Anyway, I followed the 'Flappy Bird Style' game, built it successfully and have been succeeding in adding extra elements to it and learning the code as I'm doing so.

However, I made the GameController increase the scroll speed each successful point scored (this works well, so haven't included any of the code), but this leads the spawning of columns from the object pool to spawn at increasingly large distances.

I then tried to adjust the spawn rate by decreasing it at the same rate that the score increases, however this ends up with the spawn rate being 0 and every column spawning at once.

Is there a way that I can set the columns to spawn after a set distance from the last spawned column?

If so could anyone let me know/at least point me in the right direction? Thank you in advance, and apologies, I am in my first week of C#.

ColumnPool.cs:

 using System.Collections;
 using UnityEngine;
 
 public class ColumnPool : MonoBehaviour
 {
     public GameObject columnPrefab; //The column game object.
     public int columnPoolSize = 15;
     public float spawnRate = 3f;
     public float columnMin = -1f;
     public float columnMax = 3.5f;
     private GameObject[] columns;
     private int currentColumn = 0;
 
     private Vector2 objectPoolPosition = new Vector2(-15, -25);
     private float spawnXPosition = 10f;
 
     private float timeSinceLastSpawned;
 
     void Start()
     {
         timeSinceLastSpawned = 0f;
 
         //Initialize the columns collection.
         columns = new GameObject[columnPoolSize];
         //Loop through the collection... 
         for (int i = 0; i < columnPoolSize; i++)
         {
             //...and create the individual columns.
             columns[i] = (GameObject) Instantiate(columnPrefab, objectPoolPosition, Quaternion.identity);
         }
     }
 
     void Update()
     {
         timeSinceLastSpawned += Time.deltaTime;
 
         if (GameController.instance.gameOver == false && timeSinceLastSpawned >= spawnRate)
         {
             timeSinceLastSpawned = 0f;
 
             //Set a random y position for the column
             float spawnYPosition = Random.Range(columnMin, columnMax);
 
             //...then set the current column to that position.
             columns[currentColumn].transform.position = new Vector2(spawnXPosition, spawnYPosition);
 
             //Increase the value of currentColumn. If the new size is too big, set it back to zero
             currentColumn++;
 
             if (currentColumn >= columnPoolSize)
             {
                 currentColumn = 0;
             }
         }
 
         {
             {
                 spawnRate = 4 - 0.3f * (GameController.instance.score); //<- this is where I am having trouble at the moment. 
             }
         }
 
     }
 }



Comment
Add comment · Show 1
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 TreyH · Jan 31, 2019 at 06:19 PM 1
Share

Formatted a bit for you, be sure to give a few lines between code and text. The formatting gets a bit confused sometimes. :-)

1 Reply

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

Answer by xxmariofer · Jan 31, 2019 at 07:51 PM

store a reference to the last spawnedwall, then rather checking the timeSinceLastSpawned , just change the if to

 if(GameController.instance.gameOver == false && Vector3.Distance(new Vector3(spawnXPosition, spawnYPosition, 0), referenceToTheLastObject) >= desiredDistance) //desiredDistance is the distance to you want the objects to be spawning
Comment
Add comment · Show 9 · 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 tandtgardensolutions · Jan 31, 2019 at 08:55 PM 0
Share

Thank you so much! Haven't managed to get it to work yet, but I'm sure I can given a little while longer.

avatar image xxmariofer tandtgardensolutions · Jan 31, 2019 at 10:51 PM 0
Share

whats your exact issue? i have seen in your code you already have a reference to the last object since it is already stored in the pool so it should be easy to implement, whats the part giving you issues?

avatar image tandtgardensolutions xxmariofer · Feb 01, 2019 at 01:25 PM 0
Share

Well there are a few:

The name 'spawnYPosition' does not exist in the current context The name 'referenceToTheLastObject' does not exist in the current context The name 'desiredDistance' does not exist in the current context

However as far as I can see they do exist. I think I'm storing variables and objects incorrectly in the script.

Show more comments

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

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

Related Questions

Object Pooling Help 0 Answers

Cannot convert void to UnityEngine.UI.Toggle.ToggleEvent - Javascript 0 Answers

I want Script To Move a object from a different object (In four Different directions,Randomly) . 0 Answers

How to move object a from in front of object b to in front of object c and transfer orientation? 1 Answer

Problem when instanciating objects on keypress 0 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