Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(1 edit) (+1)

Hello! I fear this answer comes too late or that I'll be 'necroing' the post (I'm really sorry if that's the case) but I've just found it and I was a little bit surprised that this got no solution posted yet or maybe because this has passed unnoticed.

The solution is actually very simple. You placed the method in the 'Item' class instead of placing it in the 'Inventory' class. The inventory is the container, so the logical thing is to place the Contains check in it.

More little things. (A little off-topic but I consider good to point out) 

You can use Lists instead of Arrays, so you can use a good bunch of already implemented functions in C# as 'Contains':

public List<Item> Inventory;
...
if(Inventory.Contains(myItem)) { ... }

You can override the Equals in the Item class too, if you consider it necessary.

Another little thing: I've noticed that you're trying to simulate properties (in this case, with getter but not setter) in the Item class.

Just to follow a better convention here on how to do things, I'd recommend you use properties with backing fields. Something like this:

[SerializeField] private string testField;
public string TestField { get { return testField; } set { testField = value; } }

You can remove the setter part in your case, of course... and about naming conventions..., follow what you find more comfortable working with.

Now... beware of auto-properties, because Unity inspectors do not show those in the Editor with Monobehaviours and ScriptableObjects:

public string TestField { get; set; }

I know it's odd, but it's a Unity thing, I had a lot of frustration myself with this too.

And here some small reading on Lists and the Array vs Lists question

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?vi...

https://www.gamasutra.com/blogs/RobertZubek/20150504/242572/C_memory_and_perform...

And some about exposing properties in inspectors

https://forum.unity.com/threads/exposing-properties-in-the-inspector.471363/

https://answers.unity.com/questions/14985/how-do-i-expose-class-properties-not-f...

Again, sorry in case of necroposting. 

Anyway, hopefully this will help you a bit.

(3 edits)

Hey Cimeto, 

Thanks for the helpful tips so far, I'll look into using Lists instead of Arrays. After posting the question, I've worked on it a little bit and noticed that I should've declared a separate class for Inventory, and use a List<Item> to represent it as you pointed out. I looked up the Contains method for the List data structure and it seems like it has the Contains() method I needed the entire time.  Whoa, what's that set and get thing, I learned about that yet. Does that automatically act as my getter and setter methods in C#? So, the auto-properties you were talking about are essentially values that are automatically assigned to empty fields right?


Thanks for the help so far :)