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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help me with simple code plz

Discussion in 'Scripting' started by pihik, Apr 25, 2022.

  1. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    Hey, can someone help me with this simple code ? When I find a new item it is added to my list but "i" is always 0, so i cannot prevent adding same item to the list



    Code (CSharp):
    1.  
    2.  
    3. public List<Item> itemsInInventory = new List<Item>();
    4.  
    5.  
    6.  
    7.     public void AddResource(List<Item> items)
    8.     {
    9.         foreach(Item item in items)
    10.         {
    11.             if (itemsInInventory.Count == 0)
    12.             {
    13.                 itemsInInventory.Add(item);
    14.             }
    15.             for (int i = 0; i < itemsInInventory.Count; i++)
    16.             {
    17.                 if (itemsInInventory.name == item.name)
    18.                 {
    19.                     Debug.Log("same name");
    20.                     return;
    21.                 }
    22.                 else
    23.                 {
    24.                     Debug.Log("find new item");
    25.                     itemsInInventory.Add(item);
    26.                 }
    27.             }
    28.         }
    29.     }
    30. }
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    itemsInInventory.name
    does this even compile? I've never seen anything like this before.

    Anyhow, the most simple way of doing it would be something like this:
    Code (CSharp):
    1. foreach (Item itemToAdd in items)
    2. {
    3.    foreach (Item ownedItem in itemsInInventory)
    4.    {
    5.       if (itemToAdd.name != ownedItem.name)
    6.       {
    7.          itemsInInventory.Add(itemToAdd);
    8.       }
    9.    }
    10. }
    The above is super ugly, but it's simple enough for you to understand without needing to explain a bunch of stuff you probably don't need to worry about now.
     
    DevDunk likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I think you just want to replace
    return;
    with
    continue;
     
  4. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    ou there was if
    Code (CSharp):
    1. (itemsInInventory[i].name == item.name)
    i probably delete it before .. and I tried this code before what you reply me, but it's not adding item to the list idk why
     
  5. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    Code (CSharp):
    1. // breaks the loop
    2. return;
    3.  
    4. // goes to next iteration { i }
    5. continue;
    when you use return, its telling the compiler the loop is finished. so it skips all other i*(n).

    untested;
    Code (CSharp):
    1. List<GameObject> example = new List<GameObject>();
    2.     public void AddToList(ref List<GameObject> new_list)
    3.     {
    4.         foreach (GameObject obj in new_list)
    5.         {
    6.             foreach (GameObject existing_obj in example)
    7.                 if (obj.name == existing_obj.name)
    8.                     continue;
    9.  
    10.             example.Add(obj);
    11.  
    12.         }
    13.     }
     
  6. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    when i replace return, than it's Adding much more items to the list
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    If the name doesn't match with any existing one, it adds the item to the list each time. You probably want to add the item after the list logic, if it is not found.
     
    Bunny83 and PraetorBlue like this.
  8. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    But I'm adding item after the logic, but If you see something wrong there, let me know I tried many things but none of it is working.
     
  9. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    No, you are adding within the foreach loop. Add it after the foreach loops if it is not found. I might suggest writing out the logic here in text, not code:

    Set isFound to false. Go through the list and compare the name you are adding to each one in the list. If it is found, set isFound = true. After the going through the list, if isFound = false, add it.
     
  10. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    With this code, there is alarm when i collect second item:

    InvalidOperationException: Collection was modified; enumeration operation may not execute.
    System.ThrowHelper.ThrowInvalidOperationException (System.ExceptionResource resource) (at <695d1cc93cca45069c528c15c9fdd749>:0)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.  
    8.     public List<Item> itemsInInventory = new List<Item>();
    9.  
    10.  
    11.     public void AddResource(List<Item> items)
    12.     {
    13.         foreach (Item itemToAdd in items)
    14.         {
    15.             if (itemsInInventory.Count == 0)
    16.             {
    17.                 itemsInInventory.Add(itemToAdd);
    18.             }
    19.             foreach (Item inventoryItem in itemsInInventory)
    20.             {
    21.                 if (inventoryItem.name != itemToAdd.name)
    22.                 {
    23.                     itemsInInventory.Add(itemToAdd);
    24.                 }
    25.             }
    26.         }
    27.        
    28.     }
    29.  
    30. }
     
  11. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    You are still adding the item within the context of the loop. I might suggest to get it working with the logic I provided.
     
  12. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    It was written before I check new replies, sorry.

    So If I understand what you mean, I write this code.. now alarm message is gone, but still second item is duplicating.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.     bool isFound = false;
    8.  
    9.     public List<Item> itemsInInventory = new List<Item>();
    10.  
    11.  
    12.     public void AddResource(List<Item> items)
    13.     {
    14.         foreach (Item itemToAdd in items)
    15.         {
    16.             if (itemsInInventory.Count == 0)
    17.             {
    18.                 itemsInInventory.Add(itemToAdd);
    19.             }
    20.             foreach (Item inventoryItem in itemsInInventory)
    21.             {
    22.                 if (inventoryItem.name != itemToAdd.name)
    23.                 {
    24.                     isFound = true;
    25.                 }
    26.                
    27.             }
    28.             if (isFound)
    29.             {
    30.                 itemsInInventory.Add(itemToAdd);
    31.                 isFound = false;
    32.             }
    33.  
    34.         }
    35.        
    36.     }
    37.  
    38. }
     
  13. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Either remove the first if statement, or even better, add an else statement between the if and foreach blocks. One learning tool is to write your lists on a piece of paper (keep it short), and go through them with the logic you have manually. Be the computer! You'll spot the issue.
     
  14. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    I'm trying, but none of it is working o.o .. I was thinking about List.Exist.. how it works ?
     
  15. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please share what you have so far. You can simply remove the first Add and if statement, the rest should work just fine. Only add at the bottom, like you have. Comment out the other Add, or remove the if statement it's in.
     
  16. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    Code (CSharp):
    1.     bool isFound = false;
    2.  
    3.     public List<Item> itemsInInventory = new List<Item>();
    4.  
    5.  
    6.     public void AddResource(List<Item> items)
    7.     {
    8.         foreach (Item itemToAdd in items)
    9.         {
    10.            
    11.             if (itemsInInventory.Count == 0) //If I delete this statement, than it cannot add any item to the list
    12.             {
    13.                 itemsInInventory.Add(itemToAdd);
    14.             }
    15.             else
    16.             {
    17.                 foreach (Item inventoryItem in itemsInInventory)
    18.                 {
    19.                     Debug.Log(itemsInInventory.Count);
    20.                     if (itemToAdd.name != inventoryItem.name)
    21.                     {
    22.                         isFound = true;
    23.                     }
    24.                     else
    25.                     {
    26.                         isFound = false;
    27.                     }
    28.                 }
    29.                 if(isFound)
    30.                 {
    31.                     itemsInInventory.Add(itemToAdd);
    32.                 }
    33.             }
    34.         }
    35.        
    36.        
    37.     }
     
  17. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Close! Remove the:

    Code (CSharp):
    1. else
    2. {
    3.   isFound = false;
    4. }
    and add:

    Code (CSharp):
    1. else {
    2.  isFound = false;
    3.  {
    4.     foreach (Item inventoryItem in itemsInInventory)
    5. ...
    6. }
     
  18. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    It's not working o.o
     
  19. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Before we go too much further down the hairy rathole of pair programming an inventory system over the forums, please keep this in mind and decide if you are prepared to tackle this level of problem today:

    There is NOTHING simple about inventories.

    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 can possess / equip
    - a database of the items you actually possess / equip
    - 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
    - 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?
    - 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?
    - can users substantially modify items with other things like spells, gems, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out?
    - 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! :)

    Breaking down a large problem such as inventory:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - Star Manta on the Unity3D forums
     
  20. pihik

    pihik

    Joined:
    Mar 15, 2022
    Posts:
    13
    Got it ... Jeez I'm dummy.. I can't search for indivitual item in inventory and than compare this individual item with itemToAdd .... and the code is so easy to understand
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.     public bool isFound = false;
    8.  
    9.     public List<Item> itemsInInventory = new List<Item>();
    10.  
    11.  
    12.     public void AddResource(List<Item> items)
    13.     {
    14.         foreach (Item itemToAdd in items)
    15.         {
    16.             if (itemsInInventory.Count == 0)
    17.             {
    18.                 itemsInInventory.Add(itemToAdd);
    19.             }
    20.             else
    21.             {
    22.                 if (!itemsInInventory.Contains(itemToAdd))
    23.                 {
    24.                     itemsInInventory.Add(itemToAdd);
    25.                 }
    26.                  
    27.             }
    28.         }
    29.        
    30.        
    31.     }
    32.  
    33. }
     
  21. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    It can be even simpler:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.     public bool isFound = false;
    8.     public List<Item> itemsInInventory = new List<Item>();
    9.  
    10.     public void AddResource(List<Item> items)
    11.     {
    12.         foreach (Item itemToAdd in items)
    13.         {
    14.             if (!itemsInInventory.Contains(itemToAdd))
    15.             {
    16.                 itemsInInventory.Add(itemToAdd);
    17.             }
    18.         }
    19.     }
    20. }
    I was just worried to suggest Contains because your previous code was comparing names, so I wasn't sure if
    Item
    was something that would play nicely with that.
     
    Bunny83 and Dextozz like this.