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 WyrmrestGames · Jul 21, 2018 at 04:00 AM · instantiateprefabpooling

[C#] Creating falling coins

So I've managed to work up this CoinPooler script for my game. Issue I'm having is none of the coins are spawning on the x axis, they are all clumped behind on another making it impossible for a player to collect the coins. Any suggestions on how to fix this? My code will be listed below.

Yes, I'm using a form of ObjectPooling and Instantiate & Destroy. I tried using normal object pooling but I kept having problems with it when I was destroying the previous game objects.

     private GameObject[] pooledBronze;
     public GameObject silverCoin;
     public GameObject goldCoin;
 
     public GameController gameControl;
 
     public GameObject bronzePrefab;
 
     private int bronzeCoinAmount = 40;
     private int silverCoinAmount = 2;
     private int goldCoinAmount = 1;
 
     private int currentBronzeCoin = 0;
     private int currentSilverCoin = 0;
     private int currentGoldCoin = 0;
 
     public float coinObjMin = -3f;
     public float coinObjMax = 9f;
     private float spawnYPos = 10;
     public float timeToSpawn = 0f;
     private Vector2 coinPoolPos = new Vector2(-3, 10);
 
 
     // Use this for initialization
     void Start () {
         gameControl.GetComponent<GameController>();
 
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         timeToSpawn += 2.0f * Time.deltaTime;
 
         if (timeToSpawn >= 9f)
         {
             spawnCoins();
             timeToSpawn = 0f;
         }
 
     }
 
     private void spawnCoins()
     {
         pooledBronze = new GameObject[bronzeCoinAmount];
         for (int i = 0; i < bronzeCoinAmount; i++)
         {
             pooledBronze[i] = (GameObject)Instantiate(bronzePrefab, coinPoolPos, Quaternion.identity);
         }
         if (gameControl.gameOver == false && gameControl.gameStart && timeToSpawn >= 60.0f)
         {
             float spawnXPos = Random.Range(coinObjMin, coinObjMax);
             pooledBronze[currentBronzeCoin].transform.position = new Vector2(spawnXPos, spawnYPos);
             currentBronzeCoin++;
 
             if(currentBronzeCoin >= bronzeCoinAmount)
             {
                 currentBronzeCoin = 0;
             }
 
             if(currentBronzeCoin == 0)
             {
                 spawnCoins();
             }
         }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kalen_08 · Jul 22, 2018 at 01:54 AM

There's a lot in there to go over... i think its been done quite poorly so maybe delete it and re-write this in a better way...

     private GameObject[] pooledBronze;
     public GameObject silverCoin;
     public GameObject goldCoin;
 
     public GameController gameControl;
 
     public GameObject bronzePrefab;
 
     private int bronzeCoinAmount = 40;
     private int silverCoinAmount = 2;
     private int goldCoinAmount = 1;
 
     private int currentBronzeCoin = 0;
     private int currentSilverCoin = 0;
     private int currentGoldCoin = 0;
 
     public float coinObjMin = -3f;
     public float coinObjMax = 9f;
     private float spawnYPos = 10;
     public float timeToSpawn = 0f;
     private Vector2 coinPoolPos = new Vector2(-3, 10);
 
 
     // Use this for initialization
     void Start () {
         // this does nothing. it gets the component but
         // does nothing with it nor assign it to something
         gameControl.GetComponent<GameController>();
     }
 
     // Update is called once per frame
     void FixedUpdate () {
 
         timeToSpawn += 2.0f * Time.deltaTime;
 
         if (timeToSpawn >= 9f)
         {
             spawnCoins();
             timeToSpawn = 0f;
         }
 
     }
 
     // overall this method is quite senseless and should be re-done
     private void spawnCoins()
     {
         var thing = new GameObject[0];
         pooledBronze = new GameObject[bronzeCoinAmount];
         for (int i = 0; i < bronzeCoinAmount; i++)
         {
             // coinPoolPos never changes which is why they all spawn in the same spot.
             pooledBronze[i] = (GameObject)Instantiate(bronzePrefab, coinPoolPos, Quaternion.identity);
         }
 
         // this block below will never get called.
         // in void FixedUpdate () if timeToSpawn >= 9 then you spawn and then set it to 0
         // therefor it will never be > 60.
         if (gameControl.gameOver == false && gameControl.gameStart && timeToSpawn >= 60.0f)
         {
             float spawnXPos = Random.Range(coinObjMin, coinObjMax);
             pooledBronze[currentBronzeCoin].transform.position = new Vector2(spawnXPos, spawnYPos);
             currentBronzeCoin++;
 
             if(currentBronzeCoin >= bronzeCoinAmount)
             {
                 currentBronzeCoin = 0;
             }
 
             // if the statement above is called then this will be called so
             // factor this out.
             if(currentBronzeCoin == 0)
             {
                 // this could end up being a recurring method == not good.
                 spawnCoins();
             }
 
             // this is a combination of the above 2 if statements into one simple one.
             if(currentBronzeCoin >= bronzeCoinAmount || currentBronzeCoin == 0)
             {
                 currentBronzeCoin = 0;
                 spawnCoins();
             }
 
 
         }
     }
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 WyrmrestGames · Jul 22, 2018 at 05:40 AM 0
Share

Okay... Well the critique is nice, however. Despite this fact, that wasn't really helpful in any way whatsoever. You say it was done "poorly" yet you offer no other advice other than "Re-do it". That doesn't really answer the question I was asking nor does it actually do anything for me. Not being a "negative nancy" or trying to be disrespectful in anyway but for future reference if you don't know what is going on then you shouldn't reply in the first place. I come here for scripting help, not the common sense gibberish I could get on Reddit. Just saying...

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

188 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 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

I get an error message every time i run this script? Any ideas? 0 Answers

Unable to set position of instantiated prefab 1 Answer

Particle Systems. Stick inside pooled objects or pool them separately. 1 Answer

My code is instantiating many prefabs, i only want one. 1 Answer

Generating prefabs at origin that are children of moving GameObjects? 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