Delivery Driver package pickup logic

I am currently trying to add some new features to the Delivery Driver project and struggling with a logic on what the next step is. Hopefully someone can give an advice.
The task is: Create a game loop where the driver (player) picks up a package and has to deliver it to a customer - an object on the map, which spawns randomely at one of the predetermined spawning points on the map.
I made a script that holds a list of positions on the map, and randomely instantiates a prefab sprite object on one of them, when Player picks up the package. This script is attached to the package pickup spot.
Now I am struggling to figure out, how to restart the circle, when the Player delivers a package to a delivery spot. By restarting a cirle I mean - hiding/deleting the active customer from the scene and be able to create a new customer position on picking up the newly spawned delivery.

1 Like

Hi OlegP,

Welcome back to our community. :slight_smile:

It’s great to see that you are challenging yourself, and your approach sounds good so far. I would have suggested predefined spawning positions but you had the idea yourself.

Now I am struggling to figure out, how to restart the circle, when the Player delivers a package to a delivery spot. By restarting a cirle I mean - hiding/deleting the active customer from the scene and be able to create a new customer position on picking up the newly spawned delivery.

A common approach to problems like this is usually to define what ‘customer’ is. And what is the difference between an active customer and an inactive/hidden/deleted customer? If you know that, the solution is often obvious.


If you don’t want to use the same spawn position twice, you could save all ‘random’ indices in an array. Then you simply increment the index which you use to access the ‘random’ indices. This means: You generate a list of random indices where you spawn your packages. You do not generate a random position index whenever the player delivered a package. The player will not notice any difference.

Here is the visualised idea:

spawnPositions: [(1,1,0), (1, 2, 0), (3, 4, 0)]
randomSpawnPosIndices: [1, 2, 0]
currentSpawnPosIndex: 0

The currentSpawnPosIndex refers to index 0 in the randomSpawnPosIndices array. The element is 1. At index 1 in spawnPositions, there is (1, 2, 0).

When you restart your level, you set the currentSpawnPosIndex to 0, and you regenerate the order of the random indices in randomSpawnPosIndices. Done.

I hope this helped. :slight_smile:

Dear Nina,

Thank you very much for your prompt reply and for a great advice on how to proceed with the logic.
For some reason the code that had been working until now stopped working properly and I have to revisit it to see what went wrong before continuing further. :slight_smile:
One last question if I may. You mentioned about the difference between an active ‘customer’ and an inactive one. As I understand, in my scenario I can not use Destroy() function to remove an instantiated
‘customer’ object, as it will be used in the same game cycle. Is the SetActive() then more preferred choice for this scenario?
Do not want to waste your time, so if the answer is complicated, could you then advise what topics I should look into and learn more about to understand the concept?
I am a bit overwhelmed with the amount of information from the Udemy course + additional resources and forums, so your guidance would be much appreciated.

Well, you can use Destroy() but it won’t destroy the customer immediately. Until the active game object gets destroyed, other game objects could still find it. A solution for this problem is to disable the game object with gameObject.SetActive(false);. A disabled object is not included in the physics simulation, and it cannot be found with the default settings of the Find* and GetComponent methods.

Depending on the size of your game, it could be an idea to reuse the customer instead of destroying and recreating it over and over again. Reusing objects is common practice to increase the performance of one’s game. In that case, you would not destroy the customer but disable it, then enable it again when needed.


If you find this a bit overwhelming, start simply. From what I read, you had the right idea, so I would suggest to make it work first. Once it works, you can decide yourself if you need to improve the performance or if the solution is good enough. Game developers usually don’t have time to optimise every single detail, so they usually try to find a good balance between ‘simple code’ and ‘performant code’. Do the same. :slight_smile:


By active and inactive customer in my previous post, I wasn’t referring to the state of the game object but to your concept. It might have been that you wanted to keep the customers in your scene the entire time. The difference between active and inactive would have been: accepting a package and not accepting a package. In that case, neither Destroy() nor SetActive() would be an appropriate solution.

It seems that I am starting to get the concept (hurrah for me, I suppose). Will try to work them in.
And thank you once again for your invaluable ideas and suggestions! You are a real treasure as an educator. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms