This is the only tutorial I've found that explained everything in most simple terms and in under 5 minutes. I'll download this video for future reference. Thanks! :)
You could also make the pool expandable in case it hits that upper limit: if (canExpand) { //All pooled objects in use. Create a new one, add it to the pool, then return it. GameObject instance = Instantiate(prefab); instance.SetActive(false); pool.Add(instance); return instance; } else { return null; }
@@dcry1003 In case if others need a solution. you create a class, then call the class before start function as an array. Basically you do the same but each time you just do for loops [System.Serializable] public class PoolList { public List pooledObjects = new List(); public int amountToPull; public GameObject EnemyPrefab; }
You don't how much I appreciate this video. I thought the 'SetActive(true)' must be on the start method of the bullet. Somehow it never sets to true. Finally, after 5 hours of googling and watching videos I can say your video is the best. Very short and straight to the point. Thank you so much!
This solution it's "Fine", but if you enable too many bullets the fps are gonna tank either way. That's because everytime you want to spawn a bullet the code >Does a forloop >Checks if the gameobject is disabled I tell you this because it happened to my when I tried to make a bullet hell game, this solution it's really bad for that since after 1k bullets the CPU starts to tank. Sadly the best solution i've found it's convoluted and uses like 6 different arrays but it reduces the forloops to only 2 (You could make it 1 but I got lazy) after the object pool spawns every object at least once and it doesn't need to check if the object is disabled because it always is.
Fantastic. Is object pooling good for enable/disable coins to get performance? I believe you can Do this with many objects that are in a game scene dozens of times.
I am using Unity 2021.3.4f1 (LTS) and getting this error when trying to declare 'public static ObjectPool instance;': CS0246 The type or namespace name 'ObjectPool' could not be found (are you missing a using directive or an assembly reference?) . I am not sure if I need a to use a different Unity class other than UnityEngine in my newer version, but please help.
this is pool but not proper pool like if you have bullet moving with rigid body and your using this method once bullet spawned then its rigid body movement is set to specific amount then when you set active it as false and then respawn bullet then rigid body will be the last time movement set to it then its movement got disturbed and here's is solution if you set the velocity zero again and respawn it it will be spawned with zero velocity and have to set velocity again so object pool can work better if you used queue or you can add objects and then remove it like get those objects after setting it as false when you add it to queue again it will be like new object i hope im not wrong please correct me
Hello! I have a question. Can I have a prefab that is basically empty of data except for the script in charge of it's velocity and gravity, and then pool that and when instantiated by the entity using it, it gets the data from a Scriptable object?
so for some reason when I do this, the bullet is no longer shooting from in front of the rocket, but wherever it is placed in the world in the beginning
In the console I get the following error: UnassignedReferenceException: The variable bulletPosition of Loli has not been assigned. What do I have to put in the "bullet position" box?
This is a pretty clean tutorial. Thank you. For real, really good. I´ve got a problem and after hours and hours (you know the drill) I decided to come here. The problem is once my bullet is not active (2s timer BulletBehaviour script) I can't use the pool anymore. If i fire fast, i get to use a lot of bullets from the pool, but once any bullet timer is reached, that's it, can´t use any more bullets from the pool. Please, please, help me. I am almost returning to the old instantiate and destroy.
@@bendux Deleted the code and returned to the old method... I will remake it now that you are willing to help. But I literally copied your stuff, even the names. This make 0 sense. I´m trying to make a topdown shooter, so i also get the position and angle from the firepoint, but that's pretty much everything different from the video. There are three scripts. BulletBehaviour that, for now, counts from 2 to 0. When it reaches 0 the bullet is set to be inactive. I think the problem is there. Since the script is pretty short, I will copy it here. float life = 2; // Update is called once per frame void Update() { if(life > 0) { life -= Time.deltaTime; } else { //Destroy(this.gameObject); gameObject.SetActive(false); } } The pooling script is a copy of yours. The spawning script also uses pretty much your logic. Used a Debug.Log to see if the problem was making the bullet a child of the canvas. Didn´t seem like it, problem stayed even if this line was //.
Hello, I hope this comment is seen. I have a question, you will see in your example game the player shoots in one direction, but in my game my character shoots in 2 directions, so when applying your code he only shoots to the right. Is there a solution for that? thank you.
Creating objects while you play will slow down the game like lagging. Specially when creating a lot of bullets at the same time. By creating them at start and just enable and disable you can prevent that lag
Because instantiating costs calculs and if you know you will instantiate some objects a lot of times that can impact performances meanwhile you could reduce a lot those calculs by doing them on a limited amount of objects at the start (where also potential performance impacts are much less annoying) and keep these objects in memory while they are unused. Just like for the variables you set in a script outside of a function, they are kept in memory and not created each time a function using them is running (among other utilites) This is however less useful if you are making a pool so big it fills the device memory, but at this point you are anyway getting big lag spikes or memory filled if not doing more specific optimizations in addition
@@lionliing.gamedev Why do we need to pre-create around 20 objects and store them in a pool as shown in the video? We could just create them when needed, right?
So simple this stuff and nice too. anyway is there possible to instantiate UI buttonprefab by this pooling and click it one by one of button to instantiate and transform to another panel and when no need in that panel we can destroy too and back to instantiate again? thanks.
The answer to your question goes beyond the scope of a RU-vid comment, but I'd still like to help you. Join our Discord server, and let's solve your problem together!
The answer to your question goes beyond the scope of a RU-vid comment, but I'd still like to help you. Join our Discord server, and let's solve your problem together!
i'm making everything right but i have one problem when i'm shooting bullet is getting setactive true but not reseting to setactive false and bullet is freezing and have delay when i'm shooting@@nuin9937
I feel like people who search for object pooling already know why it's good and you spent the first 60 seconds for nothing. I'm planning to start a channel like yours. 200 views seem so low for the effort. Maybe this is not the most popular topic. Good luck if you still do youtube, Idk honestly 3 months passed. Also, id like to know why you started youtube? to market your indie games?
People who explicitly search for object pooling probably won't need an explanation, but don't forget about the people who randomly click on this video. I mainly started with RU-vid to help others make games. Good luck!