• 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 fireblood1234 · Feb 02 at 06:35 AM · multiplayerpoolingtop down shooter

Help Please How to make the pooled object (bullet) disable after 10 seconds

I've been making a multiplayer top-down tank game, and I'm having a problem to make a script for this situation. i have a script of object pooling to my bullet so we can save memory, and we need to disable it after 10 seconds after firing. And also we want to limit the bullet to 5, so if the bullet is less than 5, you can still fire, if its already 5, you will not be able to fire (unless your bullet count became less than 5).`

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ObjectPool : MonoBehaviour { [SerializeField] protected GameObject objectToPool; [SerializeField] protected int poolSize = 10;

 protected Queue<GameObject> objectPool;

 public Transform spawnedObjectsParent;

 private void Awake()
 {
     objectPool = new Queue<GameObject>();

 }

 public void Initialize(GameObject objectToPool, int poolSize = 10)
 {
     this.objectToPool = objectToPool;
     this.poolSize = poolSize;

 }

 public GameObject CreateObject()
 {
     CreateObjectParentIfNeeded();

     GameObject spawnedObject = null;


     if (objectPool.Count < poolSize)
     {
         spawnedObject = Instantiate(objectToPool, transform.position, Quaternion.identity);
         spawnedObject.name = transform.root.name + "_" + objectToPool.name + "_" + objectPool.Count;
         spawnedObject.transform.SetParent(spawnedObjectsParent);
         spawnedObject.AddComponent<DestroyIfDisabled>();
     }
     else
     {
         spawnedObject = objectPool.Dequeue();
         spawnedObject.transform.position = transform.position;
         spawnedObject.transform.rotation = Quaternion.identity;
         spawnedObject.SetActive(true);
         StartCoroutine(DeactivateBullet());
     }

     objectPool.Enqueue(spawnedObject);
     return spawnedObject;
 }

 private void CreateObjectParentIfNeeded()
 {
     if (spawnedObjectsParent == null)
     {
         string name = "ObjectPool_" + objectToPool.name;
         var parentObject = GameObject.Find(name);
         if (parentObject != null)
             spawnedObjectsParent = parentObject.transform;
         else
         {
             spawnedObjectsParent = new GameObject(name).transform;
         }

     }
 }

 private void OnDestroy()
 {
     foreach (var item in objectPool)
     {
         if (item == null)
             continue;
         else if (item.activeSelf == false)
             Destroy(item);
         else
             item.GetComponent<DestroyIfDisabled>().SelfDestructionEnabled = true;
     }
 }
 private IEnumerator DeactivateBullet()
 {
     yield return new WaitForSeconds(10f);
     spawnedObject.SetActive(false);
 }

}`

Comment
Add comment · Show 8
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 MilitaryG · Feb 02 at 07:26 AM 0
Share

Have you tried pooling it after it hits the target?

avatar image fireblood1234 · Feb 02 at 11:00 AM 0
Share

@MilitaryG @Bunny83 hi bunny i saw your an expert hehe could you help me? Hi i dont know what you mean, im a bit new to this, so what i mean or if you want preference is like the old game named az tank or tank trouble which the limit of bullet is 5 and cannot fire if its 5, if its less than 5 bullets(fired) you can fire again, hence, the object pool, i remember when the first pooling is it disable after 10 seconds, i have 10 seconds timer , (in another script) kinda bit messy, but when its recycled it doesnt disable anymore after 10 seconds, and now even the first 5 bullets fired dont disable anymore,

avatar image xxmariofer fireblood1234 · Feb 02 at 11:07 AM 0
Share

for disabling the object you would need to change the code this

  private IEnumerator DeactivateBullet(GameObject bullet)
  {
      yield return new WaitForSeconds(10f);
      bullet.SetActive(false);
  }

  StartCoroutine(DeactivateBullet(spawnedObject));

but your script is still missing the full object pooling functionality, you should be saving the active bullets in a different list, so that you can return it to the bullets queue once it gets disabled, check this video to understand the concept behind object pooling

https://www.youtube.com/watch?v=tdSmKaJvCoA

avatar image fireblood1234 · Feb 02 at 02:36 PM 0
Share

But where should i put it? I watched the video, i dont understand what you mean, i followed some and add it to my script after watching the video, the problem now is where should i put the deactivator or the deactivatebullet

I have the turret script, bullet script, and object pooling script,

All my script is kinda copied from others because im still new and dont understand much, hehe. @xxmariofer

avatar image xxmariofer fireblood1234 · Feb 03 at 07:07 AM 0
Share

just change your DeactivateBullet method with the DeactivateBullet I posted, and the startcoroutine of your method with the startcoroutine I posted

avatar image fireblood1234 xxmariofer · Feb 03 at 08:17 AM 0
Share

it works on the first 5 bullets but when i fired again it doesnt work , also we've been having problem called nullreferenceexception i don't know where it came. if you want i can give you details that you want

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

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

How do I scale my prefab over the network? 0 Answers

Object Pool on Server not spawning on new Clients (Mirror) 0 Answers

Unity networking tutorial? 6 Answers

Shooting projectiles in a 2d top down shooter with multiplayer 1 Answer

Top Down 2D Network Transform 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