#Object Pooling

1 messages · Page 1 of 1 (latest)

lunar timber
amber junco
#

alright

lunar timber
#

Learn how to create an object pooling system to reduce processing time and help your game run smoother!

Get the scripts used in this series by supporting on Patreon at:
http://www.patreon.com/gamesplusjames

Get the art asset's used in this episode from Kenney here: http://www.kenney.nl/assets/platformer-pack-redux

Don't forget to hit that lik...

â–¶ Play video
amber junco
#

so if i understand this correct. it is working on object1 but not on object2?

lunar timber
#

oh yeah, im following this from this guy's tutorial

#

basically the latter will generator

#

at first, i did only ONE gameobject

#

something like this

#

i would keep on using instantiate, but that would create "memory" garbage i guess

amber junco
#

why are you getting so many red lines?

#

that is indeed correct

#

could you show me your scene please? then i know what i am dealing with

lunar timber
#

just a sec

#

it supposed to spawn 2 cyan blocks

#

but based on "debugging" it only spawns one

#

the top one, which is the latter code

#

since i gave the "pooled amount = 5" it should have sufficient resource to get TWO blocks out

amber junco
#

alright but the list is showing you have 5 objects

lunar timber
#

yup, that should be working correctly, since i made 5 objects to be reused again and again

amber junco
#

ah like that and then you have it so that it enables 2 of the objects but it only enables one?

lunar timber
#

correct

amber junco
#

i think the issue is with GetPooledObject()

lunar timber
#

tbh im puzzled. Because i rarely use this pooling method

amber junco
#

oh wait i foudn it

#

so it is quite easy.. you call that function after each other. But!

the mistake you made is that you dont enable the first one. so when you call the second one. it grabs the first one because it is still inactive

lunar timber
#

hmm, but i set it active first though

#

then only go on to perform the 2nd grabbing

amber junco
#

there you go this should fix it

#

what i did this fix this.
GameObject newRoomRight2 = theRoomPoolerRight.GetPooledObject();
was put AFTER the newRoomRight1 was enabled

#

and since it is enabled theRoomPoolerRight.GetPooledObject(); sees that the first one is enabled. and moves on to the second one

lunar timber
#

hmm, so that means i shouldnt "grab" both at the start

#

and instead grab 1 , set active, grab 2 set active

#

yup it worked

amber junco
#

yes

#

because it all works in order from top to bottom

lunar timber
#

i see

#

well now i think i need another scrip to prevent overlapping

amber junco
#

yeah i think so

lunar timber
#

i mean literally overlapping in the scene

amber junco
#

i do think this thread can be archived as the issue has been fixed

Yeah their position is overlapping you mean?

lunar timber
#

yup

#

so i might need something to check whether there are any "items" at this location, if yes then dont instantiate, if no, then instantiate

amber junco
#

maybe your roomwidth is 0?

lunar timber
#

nah it's correctly placed at 10

#

just that my spawn point is based on my character movement

amber junco
#

you can check if there is any object around 10f of the object

lunar timber
#

probably object with "tag" right ?

amber junco
#

yes but i would recommend using a layer for this one as they are dungeon rooms right?

#

it would be best to have the rooms itself as a layer

lunar timber
#

well yeah im using layers

#

all of them are named "platform"

amber junco
#

oh even better

#

you can just search by name

lunar timber
#

im just not sure where this thing should go

#

i have too many scripts that i cant keep track of. But i have a feeling it should be at the RoomGeneratorRight script

amber junco
#

yes

lunar timber
#

probably in this section

#

maybe something like

if (transform.position dont have any object layer named" platform"
{

then generate platform

}

else

{

do nothing

}

amber junco
#

in the same function. what you do is:

RaycastHit2D _Hit = RaycastHit2D(newRoomRight2.transform.position,transform.right * 10f;
if(_Hit)
{
  if(_Hit.CompareTag("") //Or layer)
  {
    //Execture
  }
}
lunar timber
#

if hit , then destroy item, or better, set inactive

amber junco
#

this should actually be called to the room left of the room that you are gonna move there

lunar timber
#

i could deal with the positioning. but i have to get the correct function right 1st

amber junco
#

no then move. you dont need to move or enable if there is something else already there

lunar timber
#

oh, my " RoomSpawnPoint" must always be moving with respect to the character, due to the nature of the room generation

#

it must move regardless

#

or else, it might cause the condition where the character and the room generation point to be too far apart

#

making it seem like no room are spawning near the player

amber junco
#

ah

#

in that case yes

lunar timber
#

jesus , never thought making these random room generator is that complicated.

#

and I only started unity less than 1 month ago

amber junco
#

yeah its a tough one

#

my first project was a 2.5D platformer

#

because it is 3D it is a bit easier and it was made for pc so i didnt have a lot of FPS loss

lunar timber
#

RaycastHit2D(transform.position,transform.right * 10f)

#

but what i am worried is ...... it might hit something else

#

because all of the room are of the same size

#

cant it not detect the whole room "prefab" as an item instead ?