Combine List and Array- C# Skill in Unity: 7

Objective: a quick example to demonstrate how to use both list and array as a game core function.

We have explained what is list in the last tutorial. In this one, we will make a small demo of class/ gameobject sending between list and array as an item in RPG game.
We will start with 3 script: Item, ItemDB, and Player.
And 2 gameobjects with script attached: ItemDB & Player.

Item class and list

Open the created script Item and remove MonoBehaviour. Then create 3 variables to declare an item.

Don’t forget to make this class serializable.

Open ItemDB script and create a list of Item.

Next, we will need to create 2 public methods: to add and remove an item. This will process when Player trying to grab an item or lose an item.

We will finish these 2 methods later. Now we can back to editor and create some basic items to database.

Player with limit storage slots

With our Player script, let’s create an array as inventory and set it to maximum 10 slots only.

With this script, we will make it add an item to Player when pressed “E” key, and remove an item when pressed “R” key.
First we will need to get the reference of ItemDB, then we can call the public methods that we created in Update().

And the preparation is ready, we can make Player to receive the item from database.

Get the right item

The concept of getting the right item from database to Player is sending the item ID from Player to ItemDB, when there is a match to database, return the item.
First we need to make the methods in ItemDB to pass in the id.

Then we will check if the id is matching to any item in list.

If there is a match one, we should make a same item to inventory of Player. Thus, we need to pass in the reference of Player script.
Once we pass in reference of Player, we can set the item to its inventory.

Let’s call this method from Player to get the item with id 0.

Try it and check the result.

It works! Now we can move on to remove this item.

Remove an item from Player

This part would be pretty much the same as adding an item.
Let’s finish the method of RemoveItem().

To remove the item, we just need to make the column of that item becomes a null object.

And again, call this method from Player with id number.

That’s all! Test it and you should see the item is perfectly transport between database and inventory.




A passionate gamer whose goal is to work in video game development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Backstage Weekly 18 — How Spotify drove Backstage adoption

#BuildingTeams #Blog1 | Introduction to the series

Managing Kafka Connectors at scale using Kafka Connect Manager(KCM)

Passing the AWS Certified Solutions Architect — Associate Exam

Introducing Multiple ModelCheckpoint Callbacks

Android 9 (PIE): Neither Pancake Nor Pineapple, Say Hello to Pie

📍[Round 4] “Staking Double chance” Winners announcement

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
S.J. Jason Liu

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

More from Medium

Begin your C# Game Development Journey Using This Challenge

Custom Dynamic Ambient Systems

Making Tic Tac Toe Using Unity 2D and C#

Scaling Bar HUD for Thrusters