Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by raycosantana · Oct 02, 2013 at 12:14 PM · listsfindcustom class

check if something is on a list by one argument

I've looking for a way to do this, I created a custom class for the Items stored in the players inventory (a list), but I want to check if the Item is there using its "name" argument so if it is not there add it, and if it is there augment its quantity by +1

I've look into List.Contains but since what I need to compare it to its not of the same class I cant use it.

here the code of the Inventory manager (ignore the comments, they are not mine)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 // This behaviour script would be attached to the player
 public class InventoryManager : MonoBehaviour {
   // stores items picked up
   private int ItemID;
   public AudioClip[] PotionSounds;
   private List<IItem> itemsInInventory = new List<IItem>();
   void Update() {
         Debug.Log(itemsInInventory[0].name + "_" + itemsInInventory[0].Cantidad);
         if (Input.GetButtonDown("use")== true){
             Use();
             
         }
         if (itemsInInventory[ItemID].Cantidad == 0){
              itemsInInventory.RemoveAt(ItemID);
         }
     // check for use input to use item here, or in OnGUI, and call Use()   
   }
     public void AddItem(GameObject Item){
       // We cast to IItem to match our list type. 
       // We can do this because Potion implements IItem.
       // This means we could store different types of items in the list, 
       // as long as they implement IItem.
         Potion ItemScript = Item.GetComponent<Potion>();
         itemsInInventory.Contains
         
         itemsInInventory.Add(new IItem(Item.name,ItemScript.Cantidad,ItemScript.Icon,ItemScript.PotionValue, ItemScript.PotionParticles,ItemScript.Type));
         }  
         
         
         
         
         
           
         // adds new item to end of list
     
 void Use() {
         if (itemsInInventory[ItemID].Cantidad > 0){
         if (itemsInInventory[ItemID].Type == "Potion"){
     GameObject Player = GameObject.FindGameObjectWithTag("Player");
     Player.SendMessage("AddjustCurrentHealth", itemsInInventory[ItemID].Value);
     Player.audio.PlayOneShot(PotionSounds[(Random.Range(0,PotionSounds.Length))]);
     Instantiate(itemsInInventory[ItemID].Particles, Player.transform.position, Quaternion.identity);
     itemsInInventory[ItemID].Cantidad += -1;
         }
     }
     //if (itemsInInventory.Count > 0) {
       // execute UseItem method of first item in list
       /*itemsInInventory[0].UseItem();
       // remove item from list since it has been used
       itemsInInventory.RemoveAt(0);*/
     
     }
 }

IItem class:

 // Interface, containing only signature (design), not implementation
 using UnityEngine;
 using System.Collections;
 
 public class IItem  {
   // classes that implement Item should define this method
   public string Type;
   public string name;
   public int Cantidad;
   public Texture2D Icon;
   public int Value;
   public GameObject Particles;
     public IItem(string newName, int newCantidad, Texture2D newIcon, int newValue, GameObject newParticles, string newType){
         name = newName;
         Cantidad = newCantidad;
         Icon = newIcon;
         Value = newValue;
         Particles = newParticles;
         Type = newType;
         
         
     }
 }

Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · Oct 02, 2013 at 12:26 PM

Use Linq:

    using System.Linq;

    ...

    if(!itemsInInventory.Any(i=>i.name == "somename"))
    {
       //Add it?
    }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image raycosantana · Oct 02, 2013 at 12:43 PM 1
Share

Thanks a lot!, hey, you are from Unitygems.com right? love you tutorials!

avatar image whydoidoit · Oct 02, 2013 at 12:45 PM 0
Share

Thanks man :)

avatar image raycosantana · Oct 02, 2013 at 12:58 PM 0
Share

I have a doubt though, "the its not there","is there" part is clear but when the item is there, how can I know which of the item on the list is it to add +1 to its quantity? for example

 if(itemsInInventory.Any(i=>i.name == "somename"))
    {
     itemsInInventory[x].Cantidad += 1;
 }

which Item is "x"?

avatar image raycosantana · Oct 02, 2013 at 01:39 PM 0
Share

I kind of solved my own doubt, dont know if its the best solution, I used a for loop to find the Item that has the same name:

 for(int t = 0; t < itemsInInventory.Count; t++) {
         if (itemsInInventory[t].name == Item.name){
                 itemsInInventory[t].Cantidad += 1;
           return;
              
 
     }
         }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Class List - Passing the Class Vs Passing an Index 0 Answers

Does a new instance of a custom class return null? 2 Answers

C# find specific piece of data and where it is in list 1 Answer

Gather all GameObjects with a certain name in a Scene and Apply them to a list? 3 Answers

Finding things with Raycasthit 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges