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 /
avatar image
0
Question by wenson16 · Jul 22, 2016 at 09:54 PM · instantiate prefabobject pool

Object Pooling issue. GameObject instantiating 1 bullet instead of multiple bullets

So I have this GO which supposed to shoot multiple bullets everytime it changes its designated spot/position. Unfortunately, it only instantiates one bullet everytime it changes position. I'm using object pooling to instantiate the bullet. I think the issues here are my loops and coroutine but can't still figure it out.

Here's my messy code. PLEASE TAKE NOTE: I didn't include all the variables here but I declared everything in my actual code, so don't worry about it.

 public GameObject bullet;                                                                          void Start ()
     {
         // OBJECT POOLING RIGHT HERE!!!
         // To store pooledAmount of game objects once the game starts 
         pooledAmount = 50;
         bulletS = new List<GameObject> ();
         for (int i = 0; i < pooledAmount; i++) {
             GameObject obj = (GameObject)Instantiate (bullet);
             obj.SetActive (false);
             bulletS.Add (obj);
         }
                StartCoroutine  ("Pattern");
      }
 
 IEnumerator Pattern ()
     {    
         // First Pattern Spot
         while (transform.position != patternSpot [0].position) {
             transform.position = Vector2.MoveTowards (transform.position, posXY, movementSpeed * Time.deltaTime);
             yield return null;
         }
 
         StartCoroutine ("Shoot", 1f);
         yield return new WaitForSeconds (3f);
 
         // Second Pattern Spot
         while (transform.position != patternSpot [1].position) {
             transform.position = Vector2.MoveTowards (transform.position, posXY1, movementSpeed * Time.deltaTime);
             yield return null;
 
         }
 
         StartCoroutine ("Shoot", 1f);
         yield return null;
 }
 
 IEnumerator Shoot () {
         //Don't pool any game objects that is already active
         for (int i = 0; i < bulletS.Count; i++) {
         if (bulletS[i] != null) {
             if (!bulletS [i].activeInHierarchy) {
                 bulletS [i].transform.position = transform.position;
                 bulletS [i].transform.rotation = transform.rotation;
                 bulletS [i].SetActive (true);
                 break;
                 }
             }     yield return new WaitForSeconds (.1f);
         }
     }
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
2
Best Answer

Answer by ScaniX · Jul 22, 2016 at 11:50 PM

Is the Shoot() method supposed to activate more than one bullet? If so, you should remove the break in line 45 that exits the loop after the first bullet creation.

I don't know how many bullets you want to create each time. You maybe want a second counter to break if for example 5 bullets have been created.

  IEnumerator Shoot () {
      //Don't pool any game objects that is already active
      int createdBullets = 0;
      for (int i = 0; i < bulletS.Count; i++) {
          if (bulletS[i] != null) {
              if (!bulletS [i].activeInHierarchy) {
                  bulletS [i].transform.position = transform.position;
                  bulletS [i].transform.rotation = transform.rotation;
                  bulletS [i].SetActive (true);
                  if (++createdBullets == 5) break;
              }
          }
          yield return new WaitForSeconds (.1f);
      }
  }

(I guess there is another part somewhere that disables the bullets again?)

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 wenson16 · Jul 23, 2016 at 12:15 AM 0
Share

Wow this works perfectly! Thank you so much.

avatar image wenson16 · Jul 23, 2016 at 01:56 PM 0
Share

So I have a little issue again. It seems like when I add another two or more spots to change its position, it doesn't instantiate multiple bullets accurately. The first two spots are a good. It instantiates, lets say 20 bullets per spot. However, on the third spot, it doesn't instantiate any. Then on the 4th spot it will only instantiate less than 10 bullets. The coroutine should be instantiating 20 bullets whenever the game object change its position but it's not.

Here's my code for the coroutine:

     IEnumerator Pattern ()
     {    
         // First Pattern Spot
         while (transform.position != patternSpot [0].position) {
             transform.position = Vector2.$$anonymous$$oveTowards (transform.position, posXY, movementSpeed * Time.deltaTime);
             yield return null;
 
         }
 
         StartCoroutine ("Shoot");
         yield return new WaitForSeconds (3f);
 
         // Second Pattern Spot
         while (transform.position != patternSpot [1].position) {
             transform.position = Vector2.$$anonymous$$oveTowards (transform.position, posXY1, movementSpeed * Time.deltaTime);
             yield return null;
 
         }
 
         StartCoroutine ("Shoot");
         yield return new WaitForSeconds (5f);
 
         // Third Pattern Spot
         while (transform.position != patternSpot [2].position) {
             transform.position = Vector2.$$anonymous$$oveTowards (transform.position, posXY2, movementSpeed * Time.deltaTime);
             yield return null;
         }
 
         StartCoroutine ("Shoot");
         yield return new WaitForSeconds (5f);
 
         // Fourth Pattern Spot
         while (transform.position != patternSpot [3].position) {
             transform.position = Vector2.$$anonymous$$oveTowards (transform.position, posXY3, movementSpeed * Time.deltaTime);
             yield return null;
         }
 
         StartCoroutine ("Shoot");
         yield return new WaitForSeconds (5f);
 
         // Back to First Pattern Spot
         while (transform.position != patternSpot [0].position) {
             transform.position = Vector2.$$anonymous$$oveTowards (transform.position, posXY, movementSpeed * Time.deltaTime);
             yield return null;
         }
 
         StartCoroutine ("Shoot");
         yield return null;
     }

I also have a simple code attached to the bullet below. I'm not sure if it affects the behavior of the bullets.

     void OnEnable () {
         Invoke ("Destroy", 3f);
     }
 
     void Destroy () {
         gameObject.SetActive (false);
     }
 
     void OnDisable () {
         CancelInvoke ();
     }
avatar image ScaniX wenson16 · Jul 23, 2016 at 02:55 PM 0
Share

Well the original pool you posted allows 50 bullets to be used at the same time.

I am not sure if OnEnable() is called when you activate the GameObject. You should probably explicitly call a method to start the deactivate timer on your bullet whenever you activate them.

Try using breakpoints or pause the game and check your scene hierarchy. You could also dump the bullets and their state in the Shoot() method to see if there are any available.

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

Problem instantiating a prefab, and storing a reference to it. 1 Answer

How to instantiate an object on multiple positions? 0 Answers

aim instantiated GUI text at camera 0 Answers

When attempting to place an object from prefab, the object continually jumps up to the camera and back down quickly. 0 Answers

Argument Exception: The Object you want to instantiate is null (A new one?) 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