I have an inventory script and I am trying to look through the list while the Game is running and check for a GameObject in the list with the tag Tool. I am kind of stumped on this, though I am sure there is an easy explanation.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup : MonoBehaviour
{
private Inventory inventory;
public GameObject itemButton;
private void Start()
{
inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
}
private void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player") )
{
for (int i = 0; i < inventory.slots.Count; i++)
{
if (inventory.isFull[i] == false)
{
//ITEM CAN BE ADDED
inventory.isFull[i] = true;
Instantiate(itemButton, inventory.slots[i].transform, false);
Destroy(gameObject);
break;
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public bool[] isFull;
public List <GameObject> slots;
}
slots being what is in the slot at a given time of course.
I though possibly using the Compare tag like this inventory.slots*.CompareTag(“Tool”)* Or using the if(inventory.slots.Contains()) but that will not work because when I drop an item from the inv / pickup the item, its a clone and the gameobject will not carry over in the inspector. Considering what can be in the list is technically infinite, because I have tools, weapons, wood, fish etc, id like to find out how I can search through the list for something specific. Any ideas or pointers on where to look would be great! Consider
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, especially any errors you see
links to documentation you used to cross-check your work (CRITICAL!!!)
If you’re just getting an error you failed to mention, remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
The complete error message contains everything you need to know to fix the error yourself.
The important parts of the error message are:
the description of the error itself (google this; you are NEVER the first one!)
the file it occurred in (critical!)
the line number and character position (the two numbers in parentheses)
also possibly useful is the stack trace (all the lines of text in the lower console window)
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.
Thank you for the reply! I feel I did adequately ask my question, so I am sorry it did not read clearly, unfortunately I might not be good enough with this whole programming thing yet to really explain so I will list it out.
what you want
(To pull data from the inventory.slots in the Pickup.cs and determine if it has a GameObject with a Specific Tag) - what you tried (As mentioned above I have tried using .CompareTag and .Contains - I have tried using these in different ways and the only one I see working is .Contains but I am unsure of how to apply, once again I am pretty fresh and learning my way around - what you expected to happen (Specifically in the Pickup.cs script it checks if a slot is full, if not, then add the item to the slot and so on. What I would like to happen is when an item is added to the inventory, to check if that item has a specific tag(for example Tool) and if so then execute a separate function after adding to the inventory like for example, if the item does have the Tool tag then to set a var of hasTool to true.) - what actually happened, especially any errors you see (Nothing has happened because I cannot find a good resource that applies partly due to me not knowing what to look up. There is no error, in game my inv system works great. I pick up items and can drop them, and in the inventory I can click and use them.) - Something I found https://answers.unity.com/questions/1307465/check-if-a-object-with-a-specific-tag-exists-in-an.html This seems to answer my question, I understand it in theory, and I am trying to work it out now, but this might be the way to go. Loop through the Slots array and look for a tagged item. In general from what I understand arrays such as lists and what not are simply just C# and dont have any Unity Docs because its not an api Unity created. This is me asking for help on how to achieve something.
Well to answer your question, if you have a list of game objects you can go List.Find with a lambda, checking the tag, such as: GameObject tool = slots.Find(x => x.CompareTag("Tool"));
However you’re going to have a hard time handling an inventory with items as game objects. This isn’t a criticism, it’s advice to stop you from going down this route too far.
With an OOP language like C# you want the information to be on the objects themselves, so you’re going to want to express information and behaviour on them and have a way to author this. The most straight forward way to do this in Unity is with scriptable objects.
This is to say there’s a lot of learning ahead of you.
These things (character customization, inventories, shop systems) are fairly tricky hairy beasts, definitely deep in advanced coding territory. They 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 an inventory 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.
Or… do like I like to do: just jump in and make it up as you go. It is SOFT-ware after all… evolve it as you go!