trying to make a game where the player adds fruits to a plate. I used this code below, I’m not to familiar with lists, but I’d like to use them so I can make recipes easier to code in
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class ShowPlateContents : MonoBehaviour
{
public GameObject Plate;
public List<GameObject> PlateContents = new List<GameObject>();
public GameObject TomatoG;
public GameObject TomatoB;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void AddContent(string content)
{
if (content == "TomatoGood")
{
GameObject newFood = Instantiate(TomatoG);
newFood.transform.position = new Vector2 (0, -3);
//PlateContents.Add(newFood);
} else if (content == "TomatoBad")
{
GameObject newFood = Instantiate(TomatoB);
newFood.transform.position = new Vector2(0, -3);
PlateContents.Add(newFood);
}
}
public void ResetPlate()
{
foreach (GameObject food in PlateContents)
{
Destroy(food);
}
}
}
PlateContents is the GameObject list in this scenario
In addition to the suggestion of reading a bit more about C# collections I will suggest a couple of things. Call them food for thought.
Consider not adding strings that imply some game object and simply add the game object itself. So instantiate a TomatoG or TomatoB or PotatoP as the case may be and add that to the plate. You don’t want more and more magic strings in a method. And (this is slightly important) you don’t necessarily want all food items to have the same position.
Hard to tell from the short example but the very important part is don’t reference everything as a game object. Create a class that defines it and use the class. And in this case they might implement an IFood interface so only food could be added to the plate.
Just for full disclosure about what you’re getting into: what you describe above is an Inventory, the plate being an inventory container, the fruit being items.
And this is a crafting system:
You actually have selected one of the hardest and most abstract areas of game coding possible. This is actually the pinnacle of complexity and difficulty, so much so that I keep an entire copy/paste handy to help people organize their thoughts before tackling these massive undertakings.
These things (inventory, shop systems, character customization, dialog tree systems, crafting, ability unlock systems, tech trees, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.
The following applies to ALL types of code listed above, but for simplicity I will call it “inventory.”
Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.
If you contemplate late-delivery of content (product expansion packs, DLC, etc.), all of that has to be folded into the data source architecture from the beginning.
Inventories / shop systems / character selectors all contain elements of:
a database of items that you may possibly possess / equip
a database of the items that you actually possess / equip currently
perhaps another database of your “storage” area at home base?
persistence of this information to storage between game runs
presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
interaction with items in the inventory or on the character or in the home base storage area
interaction with the world to get items in and out
dependence on asset definition (images, etc.) for presentation
Just the design choices of such a system can have a lot of complicating confounding issues, such as:
can you have multiple items? Is there a limit?
if there is an item limit, what is it? Total count? Weight? Size? Something else?
are those items shown individually or do they stack?
are coins / gems stacked but other stuff isn’t stacked?
do items have detailed data shown (durability, rarity, damage, etc.)?
can users combine items to make new items? How? Limits? Results? Messages of success/failure?
can users substantially modify items with other things like spells, gems, sockets, etc.?
does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
etc.
Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.
Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.
Breaking down a large problem such as inventory:
If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).
Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.
Breaking down large problems in general:
The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.