public class ProjectileShooter :
{
public GameObject projectilePrefab;
public Transform projectileSpawnPoint;
public float shootForce = 10f;
public float shootRate = 0.5f;
code
private float nextShootTime;
private ObjectPool projectilePool;
private void Start()
{
// Create the object pool for projectiles
projectilePool = new ObjectPool(projectilePrefab);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && Time.time >= nextShootTime)
{
// Shoot a projectile
ShootProjectile();
nextShootTime = Time.time + shootRate;
}
}
private void ShootProjectile()
{
// Get a projectile from the object pool
GameObject projectile = projectilePool.GetObjectFromPool();
// Set the position and rotation of the projectile
projectile.transform.position = projectileSpawnPoint.position;
projectile.transform.rotation = projectileSpawnPoint.rotation;
// Activate the projectile
projectile.SetActive(true);
// Apply force to the projectile
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.velocity = projectileSpawnPoint.forward * shootForce;
}
}
In this script, you'll need to create an empty GameObject in your scene and attach this script to it. Then, assign the appropriate values in the Inspector for theprojectilePrefab(the prefab for the projectile),projectileSpawnPoint(the Transform where the projectiles should be spawned),shootForce(the force applied to the projectile), andshootRate(the cooldown between shots).
This script assumes you have anObjectPoolclass that handles object pooling. If you don't have one, you can create your own or use a third-party object pooling library.
Make sure to adjust the script as needed to match your specific project and requirements.
User
Can you provide the ObjectPool class
Assistant
Certainly! Here's an example of a simpleObjectPoolclass that you can use for object pooling in Unity:
csharpCopy codeusing System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
private GameObject prefab;
private List objectPool;
code
public ObjectPool(GameObject prefab)
{
this.prefab = prefab;
objectPool = new List<GameObject>();
}
public GameObject GetObjectFromPool()
{
// Find inactive object in the pool
GameObject obj = objectPool.Find(o => !o.activeSelf);
if (obj == null)
{
// If no inactive object found, create a new one
obj = CreateNewObject();
}
return obj;
}
private GameObject CreateNewObject()
{
// Instantiate a new object from the prefab
GameObject newObj = Object.Instantiate(prefab<REDACTED>);
// Add it to the object pool
objectPool.Add(newObj);
// Deactivate the object initially
newObj.SetActive(false);
return newObj;
}
}
To use thisObjectPoolclass, you can create an instance of it in yourProjectileShooterscript (as shown in the previous example) and provide the projectile prefab to the constructor. TheGetObjectFromPoolmethod will return an inactive object from the pool if available, or create a new one if needed.
Make sure to adjust theObjectPoolclass as per your specific requirements. You can add additional functionalities like resizing the pool, pre-warming the pool with a specific number of objects, or customizing the pooling behavior based on your project needs.
User
Can you get it so there's a pool of 10 bullets from the start
Assistant
Certainly! To initialize the object pool with a specific number of objects (in this case, 10 bullets), you can modify theObjectPoolclass as follows:
csharpCopy codeusing System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
private GameObject prefab;
private List objectPool;
code
public ObjectPool(GameObject prefab, int initialPoolSize)
{
this.prefab = prefab;
objectPool = new List<GameObject>(initialPoolSize);
// Create and add objects to the pool
for (int i = 0; i < initialPoolSize; i++)
{
CreateNewObject();
}
}
public GameObject GetObjectFromPool()
{
// Find inactive object in the pool
GameObject obj = objectPool.Find(o => !o.activeSelf);
if (obj == null)
{
// If no inactive object found, create a new one
obj = CreateNewObject();
}
return obj;
}
private GameObject CreateNewObject()
{
// Instantiate a new object from the prefab
GameObject newObj = Object.Instantiate(prefab<REDACTED>);
// Add it to the object pool
objectPool.Add(newObj);
// Deactivate the object initially
newObj.SetActive(false);
return newObj;
}
}
Now, when you create an instance of theObjectPoolclass in yourProjectileShooterscript, you can specify the initial pool size as 10:
csharpCopy codeusing System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
private GameObject prefab;
private Transform parentTransform;
private List objectPool;
code
public ObjectPool(GameObject prefab, int initialPoolSize, Transform parentTransform)
{
this.prefab = prefab;
this.parentTransform = parentTransform;
objectPool = new List<GameObject>(initialPoolSize);
// Create and add objects to the pool
for (int i = 0; i < initialPoolSize; i++)
{
CreateNewObject();
}
}
public GameObject GetObjectFromPool()
{
// Find inactive object in the pool
GameObject obj = objectPool.Find(o => !o.activeSelf);
if (obj == null)
{
// If no inactive object found, create a new one
obj = CreateNewObject();
}
return obj;
}
private GameObject CreateNewObject()
{
// Instantiate a new object from the prefab
GameObject newObj = Object.Instantiate(prefab<REDACTED>, parentTransform);
// Add it to the object pool
objectPool.Add(newObj);
// Deactivate the object initially
newObj.SetActive(false);
return newObj;
}
}
Here, we added a new parameterparentTransformto theObjectPoolconstructor. This parameter represents the transform of theBulletManagerGameObject that you want the bullets to be children of. Then, when creating a new object in theCreateNewObjectmethod, we pass theparentTransformas the parent transform for the newly instantiated bullet.
To use this , you would create an instance of it in yourProjectileShooterscript, and provide theBulletManagerGameObject's transform as theparentTransformparameter:
csharpCopy codepublic Transform bulletManagerTransform; // Assign the BulletManager's transform in the Inspector
private ObjectPool projectilePool;
private void Start()
{
// Create the object pool with an initial size of 10 and assign the BulletManager's transform
projectilePool = new ObjectPool(projectilePrefab, 10, bulletManagerTransform);
}
Now, when the bullets are spawned from the object pool, they will automatically become children of theBulletManagerGameObject.
User
Now it causes a bug where the bullets are following the player when I move/rotate is there a way to get it to move independently while also parented
Assistant
To make the bullets move independently while remaining parented to theBulletManagerGameObject, you can adjust the script in the following way:
Create a new empty GameObject as a child of theBulletManagerGameObject. Let's call . This object will serve as the parent for all the bullets.
Modify theObjectPoolclass to include theBulletContainerGameObject as a parameter and set it as the parent of the bullets:
Create a new empty GameObject as a child of theBulletManagerGameObject. Let's call . This object will serve as the parent for all the bullets.
Modify theObjectPoolclass to include theBulletContainerGameObject as a parameter and set it as the parent of the bullets:
csharpCopy codeusing System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
private GameObject prefab;
private Transform parentTransform;
private List objectPool;
code
public ObjectPool(GameObject prefab, int initialPoolSize, Transform parentTransform)
{
this.prefab = prefab;
this.parentTransform = parentTransform;
objectPool = new List<GameObject>(initialPoolSize);
// Create and add objects to the pool
for (int i = 0; i < initialPoolSize; i++)
{
CreateNewObject();
}
}
public GameObject GetObjectFromPool()
{
// Find inactive object in the pool
GameObject obj = objectPool.Find(o => !o.activeSelf);
if (obj == null)
{
// If no inactive object found, create a new one
obj = CreateNewObject();
}
return obj;
}
private GameObject CreateNewObject()
{
// Instantiate a new object from the prefab
GameObject newObj = Object.Instantiate(prefab<REDACTED>, parentTransform);
// Add it to the object pool
objectPool.Add(newObj);
// Deactivate the object initially
newObj.SetActive(false);
return newObj;
}
}
Update theShootProjectilemethod in theProjectileShooterscript to set theBulletContaineras the parent of the instantiated projectiles instead of theprojectileSpawnPoint:
User
I wanted the bulletmanagertransform to be a child of the player and so this doesnt fix it
Assistant
I apologize for the confusion. If you want theBulletManagerGameObject to be a child of the player while still allowing the bullets to move independently, you can follow these steps:
Attach theBulletManagerGameObject as a child of the player GameObject in the Unity Editor hierarchy.
Update theShootProjectilemethod in theProjectileShooterscript to set the player GameObject as the parent of theBulletManager:
Attach theBulletManagerGameObject as a child of the player GameObject in the Unity Editor hierarchy.
Update theShootProjectilemethod in theProjectileShooterscript to set the player GameObject as the parent of theBulletManager:
csharpCopy codeprivate void ShootProjectile()
{
// Get a projectile from the object pool
GameObject projectile = projectilePool.GetObjectFromPool();
code
// Set the position and rotation of the projectile
projectile.transform.position = projectileSpawnPoint.position;
projectile.transform.rotation = projectileSpawnPoint.rotation;
// Set the BulletManager's parent as the player <REDACTED>GameObject
bulletManagerTransform.SetParent(playerTransform);
// Activate the projectile
projectile.SetActive(true);
// Apply force to the projectile
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.velocity = projectileSpawnPoint.forward * shootForce;
}
Additionally, update theObjectPoolclass to include the player's transform as a parameter:
Additionally, update theObjectPoolclass to include the player's transform as a parameter:
csharpCopy codeprivate void ShootProjectile()
{
// Get a projectile from the object pool
GameObject projectile = projectilePool.GetObjectFromPool();
code
// Set the position and rotation of the projectile
projectile.transform.position = projectileSpawnPoint.position;
projectile.transform.rotation = projectileSpawnPoint.rotation;
// Set the BulletManagerContainer as the parent of the BulletManager
bulletManagerTransform.SetParent(bulletManagerContainer.transform);
// Activate the projectile
projectile.SetActive(true);
// Apply force to the projectile
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.velocity = projectileSpawnPoint.forward * shootForce;
}
Update theObjectPoolclass to include theBulletManagerContainerGameObject as a parameter:
Update theObjectPoolclass to include theBulletManagerContainerGameObject as a parameter:
csharpCopy codeusing System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
private GameObject prefab;
private GameObject parentObject;
private List objectPool;
code
User
Forget these last few requests. I'm happy with how it is and am willing to compromise on not having the bulletmanager attached to the player
Assistant
I apologize for any confusion caused. If you are satisfied with the current setup and are willing to compromise on not attaching theBulletManagerto the player, that is completely fine.
If you have any further questions or need assistance with anything else, please feel free to ask.
User
Can you give me the complete script again for the projectile shooting with instantiated 10 bullets in the pool and being children of a bulletmanager transform
Assistant
Certainly! Here's the complete script for projectile shooting with a pool of 10 bullets, where the bullets are children of aBulletManagertransform:
csharpCopy codeusing System.Collections.Generic;
using UnityEngine;
public class ProjectileShooter :
{
public GameObject projectilePrefab;
public Transform projectileSpawnPoint;
public Transform bulletManagerTransform;
code
private ObjectPool projectilePool;
private void Start()
{
// Create the object pool with an initial size of 10 and assign the BulletManager's transform
projectilePool = new ObjectPool(projectilePrefab, 10, bulletManagerTransform);
}
private void Update()
{
if (Input.GetButtonDown("Fire1"))
{
ShootProjectile();
}
}
private void ShootProjectile()
{
// Get a projectile from the object pool
GameObject projectile = projectilePool.GetObjectFromPool();
// Set the position and rotation of the projectile
projectile.transform.position = projectileSpawnPoint.position;
projectile.transform.rotation = projectileSpawnPoint.rotation;
// Set the BulletManager transform as the parent of the projectile
projectile.transform.SetParent(bulletManagerTransform);
// Activate the projectile
projectile.SetActive(true);
// Apply force to the projectile
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.velocity = projectileSpawnPoint.forward * 10f;
}
}
public class ObjectPool
{
private GameObject prefab;
private Transform parentTransform;
private List objectPool;
code
User
Thank you very much Mr ChatGPT : )
Assistant
You're welcome! I'm glad I could assist you. If you have any more questions in the future, feel free to ask. Happy coding!
Cite this conversation
“Write me a Unity C# script for shooting projectiles using object pooling.” Conversation with text-davinci-002-render-sha, from the ShareChat — Chatbot Conversations in the Wild dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m84thhsq8g000z0myc6yw2bx
Update theShootProjectilemethod in theProjectileShooterscript to set theBulletContaineras the parent of the instantiated projectiles instead of theprojectileSpawnPoint:
csharpCopy codeprivate void ShootProjectile()
{
// Get a projectile from the object pool
GameObject projectile = projectilePool.GetObjectFromPool();
code
// Set the position and rotation of the projectile
projectile.transform.position = projectileSpawnPoint.position;
projectile.transform.rotation = projectileSpawnPoint.rotation;
// Set the BulletContainer as the parent of the projectile
projectile.transform.SetParent(bulletManagerTransform);
// Activate the projectile
projectile.SetActive(true);
// Apply force to the projectile
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.velocity = projectileSpawnPoint.forward * shootForce;
}
With this modification, the bullets will remain parented to theBulletManagerGameObject and move independently of the player's movement or rotation.
public ObjectPool(GameObject prefab, int initialPoolSize, Transform playerTransform)
{
this.prefab = prefab;
this.playerTransform = playerTransform;
objectPool = new List<GameObject>(initialPoolSize);
// Create and add objects to the pool
for (int i = 0; i < initialPoolSize; i++)
{
CreateNewObject();
}
}
public GameObject GetObjectFromPool()
{
// Find inactive object in the pool
GameObject obj = objectPool.Find(o => !o.activeSelf);
if (obj == null)
{
// If no inactive object found, create a new one
obj = CreateNewObject();
}
return obj;
}
private GameObject CreateNewObject()
{
// Instantiate a new object from the prefab
GameObject newObj = Object.Instantiate(prefab<REDACTED>, playerTransform);
// Add it to the object pool
objectPool.Add(newObj);
// Deactivate the object initially
newObj.SetActive(false);
return newObj;
}
}
By making these changes, theBulletManagerwill be a child of the player, and the bullets will move independently while remaining parented to theBulletManagerGameObject.
public ObjectPool(GameObject prefab, int initialPoolSize, GameObject parentObject)
{
this.prefab = prefab;
this.parentObject = parentObject;
objectPool = new List<GameObject>(initialPoolSize);
// Create and add objects to the pool
for (int i = 0; i < initialPoolSize; i++)
{
CreateNewObject();
}
}
public GameObject GetObjectFromPool()
{
// Find inactive object in the pool
GameObject obj = objectPool.Find(o => !o.activeSelf);
if (obj == null)
{
// If no inactive object found, create a new one
obj = CreateNewObject();
}
return obj;
}
private GameObject CreateNewObject()
{
// Instantiate a new object from the prefab
GameObject newObj = Object.Instantiate(prefab<REDACTED>, parentObject.transform);
// Add it to the object pool
objectPool.Add(newObj);
// Deactivate the object initially
newObj.SetActive(false);
return newObj;
}
}
By making these changes, the bullets will move independently of the player's movement while keeping theBulletManagerGameObject as a child of theBulletManagerContainer.
public ObjectPool(GameObject prefab, int initialPoolSize, Transform parentTransform)
{
this.prefab = prefab;
this.parentTransform = parentTransform;
objectPool = new List<GameObject>(initialPoolSize);
// Create and add objects to the pool
for (int i = 0; i < initialPoolSize; i++)
{
CreateNewObject();
}
}
public GameObject GetObjectFromPool()
{
// Find inactive object in the pool
GameObject obj = objectPool.Find(o => !o.activeSelf);
if (obj == null)
{
// If no inactive object found, create a new one
obj = CreateNewObject();
}
return obj;
}
private GameObject CreateNewObject()
{
// Instantiate a new object from the prefab
GameObject newObj = Object.Instantiate(prefab<REDACTED>, parentTransform);
// Add it to the object pool
objectPool.Add(newObj);
// Deactivate the object initially
newObj.SetActive(false);
return newObj;
}
}
Please make sure to attach this script to an appropriate GameObject in your Unity scene and assign the required public variables (projectilePrefab,projectileSpawnPoint, andbulletManagerTransform) in the Inspector.