randomly instantiate or just move items across screen when boundary reached?

hi,
I’m building a frogger rip off as the next stage in trying to learn coding without the aid of a tutorial. Currently I start the level with cars & logs onscreen that start moving when the player starts. There are spawn points offscreen that spawn new objects on random timers (so the gaps aren’t consistent between cars, logs etc) & the objects destroy when they are off screen on the other side.

My question is, if I have enough items onscreen to start with (probably more than I currently do) so that the player probably couldn’t memorise the spacings, is it better to do it the way I am or just move the items to the other side of the screen when they reach the current destroy point so that they start moving across screen again I.e. Get rid of the whole instantiate/destroy cycle & just relocate/reuse the items?

It really depends on what you’re making it for IMO. Going out of your way to re-use assets rather than Instantiating new ones is a great idea to have when making something for mobile devices, where Instantiating assets is easily one of the biggest memory/processing drains- and as an exercise (which is really the point, isn’t it?) it would be beneficial I think to learn how to manage it.

That said, if you have no intention of ever doing mobile game development, this kind of clever thinking could also easily fall into the “premature optimization” category, and possibly “needlessly complicated pseudo-optimization that doesn’t serve much of a purpose”. <= those should be really avoided like the plague in a production environment, but in a learning environment? I say “go for it”! :slight_smile:

It’s a bit of both really. General knowledge so if I ever do a mobile app I will have a start on avoiding big drains & im also having big frame rate drops even though the game is really basic with not a lot happening, low textures (256) on top of primitives etc. frame rate was fine but now when I do a build it drops to 17-22fps. Profiler shows it is 84% waiting to render (Looking at the forums others seem to have the same issue but the only common factor is windows 8.1.) but I don’t know what else I can do so I started looking for any small change I could make as my coding is almost definitely not optimised. Since the biggest non static item is all the moving stuff I thought I’d start there.

You could achieve both by using a pool of unused objects. Every time a vehicle moves off the screen, put it at the end of a large-ish queue of possible objects and grab the first random vehicle from the beginning of the queue and reposition it.

That should solve both problems.

Ok, time to go read about pools :slight_smile:

Thanks

No reason to make it too complicated with one of the dozen generic object pool implementations posted all over the web. It can be as simple as this:

// in an appropriate class like your vehicle manager or whatever
public Queue<GameObject> unusedVehicles = new Queue<GameObject>();

for(int i = 0; i < 50; ++i)
{
    GameObject vehicle = Instantiate<GameObject>(a random prefab);
    vehicle.SetActive(false);

    unsuedVehicles.Enqueue(vehicle);
}

// when a vehicle needs to spawn
GameObject vehicle = unquedVehicles.Dequeue();
vehicle.SetActive(true);
vehicle.transform.position = new Vector3(position);

// when a vehicle moves off screen
vehicle.SetActive(false);
unusedVehicles.Enqueue(vehicle);

Cool, thanks. I found Mike’s unity tutorial on pools so have bookmarked that as well. Had seen reference to pools & queues but never knew what they were for.