How to stock shelves using C#?

Janika Suhonen
3 min readMar 27, 2024


Supermarket Simulator has been a simulator fan’s favorite game after the release of its Early Access version. But what makes this game so addictive and relaxing?

In this post, I will show how to easily and fast stock the shelves. And trust me, it will be easier and faster than in real life.

What we need


  • Stocking
  • Shelf
  • Stocking Trigger
  • Grocery Item
  • Grocery Item Manager

Game Objects

Let’s start going through what the scripts do and where we attach them.

Stocking Trigger


This script enables and disables the Stocking scripts boolean when the player enters and exits the trigger so you can control when you can stock items.


  • In this script, we have Boolean canStockItems which we manage through the StockingTrigger.cs.
  • The main idea in this script is to cast a ray when the boolean is true and when we press the left mouse button.
  • If the ray hits an object that has a shelf tag — we search the object to get the Shelf.cs and save it to the shelf.
  • Then we use the function inside the Shelf.cs called StockShelfs();


  • I have 4 empty game objects which will be the position where I add the grocery items. So we create a Transform array to hold these.
  • We have itemHolder transform, which is also an empty game object. You can set it in front of the player how you like.
  • Then the currentItem is the Instantiated object which we take from GroceryItemManager.cs. With this, we can manage which items we stock. But in this example, we only stock one item.

What happens inside the StockShelfs function?

  • When we press the left mouse button down -> we go through the shelfItemSpaces array using the for loop.
  • After that we check if the shelfItemSpace transform HAS one or more child game objects, if there is a game object — we continue the loop and skip the current one.
  • If there are no child game objects we Instantiate an object to itemHolder position and assign it as a child to the correct shelfItemSpaces game object.
  • Then we set the GroceryItem.cs transform placeOnShelf to shelfItemSpace — and break from the loop so it doesn’t continue.

Grocery Item

  • After we Instantiate the grocery object we check if the object is not on the shelf and if placeOnShelf isn’t null.
  • Then we start moving the object towards the placeOnShelf using Vector3.MoveTowards.
  • When the object position equals the placeOnShelf position — we set the isOnShelf to true

Grocery Item Manager

  • We manage here the currentGroceryItem that we will Instantiate through the Shelf.cs
  • Have an array full of many grocery items to choose from
  • At start I set the currentGroceryItem to groceryItems[3] — which is the mozzarella.



Janika Suhonen

From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.