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

Resolved Trying to add GameObjects to inventory

Discussion in 'Scripting' started by universiti_com, Jul 29, 2023.

  1. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11
    I have this code that I'm using to add items. It finds the items and displays the name. But when I go to add the item to my inventory slots (automatically on Awake), it doesn't seem to work and I get a console error.

    Code (CSharp):
    1.             foreach (GameObject go in taggedObjects)  //iterate through all returned objects, and find the one with the correct tag
    2.             {
    3.                 if (go.tag == "InventoryItem")
    4.                 {
    5.                     objectImLookingFor = go;
    6.                     Debug.Log("found " + objectImLookingFor.name);
    7.                     i = i++;
    8.                     slots[i].Add(objectImLookingFor);
    9.  
    10.                     // break;
    11.                 }
    12.             }

    The error I receive is: "NotImplementedException: The method or operation is not implemented."
     
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Hi, can you copy paste the whole error and your full script? The error should include more information ;)

    You're probably calling a method whose body is empty but for a single
    throw new NotImplementedException();
    line. Your error should tell you what script that method is in.

    It'll look something like this:

    Code (CSharp):
    1.     private void SomeMethod()
    2.     {
    3.         throw new NotImplementedException();
    4.     }
    Delete the throw line and the error will go away. Of course it just won't do anything until you provide some implementation in there.
     
    Last edited: Jul 29, 2023
  3. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11
    I've attached the whole thing. The full error is:

    NotImplementedException: The method or operation is not implemented.
    XEntity.InventoryItemSystem.ItemContainer.Awake () (at Assets/XEntity GameKit/Scripts/Inventory and Item System/ItemContainer.cs:114)
     

    Attached Files:

  4. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11
    See above, it's not a NotImplementedException(). I think it's an error in another method or function but I can't seem to find it,
     
  5. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    My mistake, fixed my post.

    In your full script, given 'slots' is an array of 'ItemSlot':

    protected ItemSlot[] slots;


    Your ItemSlot script needs an implemented Add method for this to be valid:

    slots[i].Add(objectImLookingFor);
     
  6. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11

    When I add the protected line it throws tons of errors in the other methods/functions.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Whatever you're trying to call, it isn't implemented.

    Either implement it, or ask whoever wrote it to implement it for you (eg, an update to an asset).

    As always start with the documentation for the method you're trying to use.

    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.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    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.


    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.
     
  8. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Don't add the protected line, that's a quote from the script you gave me for download ;)

    Go to your script, put your caret over the ItemSlot type, and press F12. Visual Studio will open up the ItemSlot script for you. Scroll down it and find the Add function. You need to place your implementation in the Add method replacing the NotImplementedExceptionLine.
     
  9. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11

    Also I have this line already:

    Code (CSharp):
    1.         //The array of slots this container holds. The slots are assigned through code based on the number of children the slot holder Transform contains.
    2.         protected ItemSlot[] slots;
     
  10. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11
    There was already an Add function so I created an InitialAdd method. However, I can't call it from the original script.

    Code (CSharp):
    1.  public void InitialAdd(Item item)
    2.         {
    3.            
    4.  
    5.             GameObject objectImLookingFor = null;
    6.             GameObject[] taggedObjects = GameObject.FindGameObjectsWithTag("InventoryItem");  //Find all GameObjects with specific tag
    7.  
    8.             foreach (GameObject go in taggedObjects)  //iterate through all returned objects, and find the one with the correct name
    9.             {
    10.                 if (go.tag == "InventoryItem")
    11.                 {
    12.                     objectImLookingFor = go;
    13.                     Debug.Log("found " + objectImLookingFor.name);
    14.                     i++;
    15.                     slots[i].Add(objectImLookingFor);
    16.  
    17.                     // break;
    18.                 }
    19.             }
    20.  
    21.             if (objectImLookingFor == null)  //If still null, that means no object matched the tag and name criteria
    22.             {
    23.                 Debug.Log("Items with InventoryItem tag not found");
    24.             }
    25.             else
    26.             {
    27.  
    28.                 Debug.Log("Found items.");
    29.             }
    30.         }
    31.  
    32.  
    33.         public bool Add(Item item)
    34.         {
    35.             if (IsAddable(item))
    36.             {
    37.                 slotItem = item;
    38.                 itemCount++;
    39.                 OnSlotModified();
    40.                 return true;
    41.  
    42.  
    43.             }
    44.             else return false;
    45.  
    46.  
    47.            
    48.         }
     
  11. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11
    Hi Kurt,

    Since the code was implemented originally, this is wrong: "Whatever you're trying to call, it isn't implemented."

    Thanks anyway.

     
  12. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    If your Add methods inside ItemSlot class do not have any
    throw new NotImplementedException();
    lines in them, I wonder if the error might be old. Click the clear button in the console to get rid of it. If you already prodvided implementation for the Add methods in ItemSlot, then it won't reappear.

    If the error comes back when you press play, there's still an Add method in ItemSlot with a
    throw new NotImplementedException();
    line in it. I'm not aware of any other scenario that a not implemented exception would appear. Note there may be multiple Add methods. Use ctrl+f to search for the word 'throw' and see what you find in there. If the script is long, maybe you missed it :)

    If in doubt, post the script, let's see what it looks like in the ItemSlot class.
     
  13. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11
    I've posted the ItemSlot class. Not sure what's going on.

     

    Attached Files:

  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Not sure why you would post that.

    From your original message, the error is pretty clear where it blows up:

    Look there... probably about line 114 of System/ItemContainer.cs I'm guessing... :)
     
  15. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11
    I found the issue and you were right!

    But now I have a different (related) problem. It's throwing a type error. See attached,
     

    Attached Files:

  16. universiti_com

    universiti_com

    Joined:
    Jan 5, 2022
    Posts:
    11
    Hi @mopthrow, I'm marking this thread resolved since the new problem is related but not exactly the same problem. Thanks for all your help!!!
     
    mopthrow likes this.
  17. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    This one is because your ItemSlot.Add method takes a different type than what you're trying to give it. Your
    objectImLookingFor
    is a
    GameObject
    , but the Add method takes an
    Item
    .

    So you'll need to make sure the objectImLookingFor is the right type, or make another Add method that takes a GameObject.

    Is this someone elses code? I think it's quite difficult to learn from someone elses premade code if you're just starting. It might be worth taking these two in order to learn to make your own:

    C# non-unity:


    And this one which is Unity specific:
    https://learn.unity.com/learn/pathway/junior-programmer

    With a short pause to work through those, you'll probably find in a few weeks/months (depending on how quickly you get through them) it'll be easier to build your own inventory system from scratch and you'll know how to avoid all the errors. Then you can build pretty much anything you want to without having to rely on using other's code. Imo at the moment you're walking a more difficult path :)