Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Creating a new Contains method to find objects of a specific type

Discussion in 'Scripting' started by thecronicreplay, Dec 21, 2020.

  1. thecronicreplay

    thecronicreplay

    Joined:
    Dec 21, 2020
    Posts:
    2
    Hey everyone!

    I found it super awesome that people here are really supportive to new game developers. I have a small question about creating new Contain methods for data structures other than primitive data types. I'll try my best to explain what I mean, I've created a Scriptable Object called Item that I want to add to my game. The new method I wanted to incorporate was ContainsItem to check if my inventory contains a specific Item.





    I was hoping to call ContainsItem to an array of Items, Item[], but it gives me this error.

    How do I resolve an issue like this? Thanks in advanced!
     
  2. MrPaparoz

    MrPaparoz

    Joined:
    Apr 14, 2018
    Posts:
    156
    But your 'Item' class has the ContainsItem method not your Inventory while it should be in 'Inventory' object.
    Why would an Item should look for itself if it's in a certain inventory?
     
  3. thecronicreplay

    thecronicreplay

    Joined:
    Dec 21, 2020
    Posts:
    2
    Ah okay, so instead of declaring it in my Item class I'll try creating a separate Inventory class to use an Item[] to store all the player's items, and move ContainsItem to my new Inventory class.
     
    MrPaparoz likes this.