• 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 Ethanbf3 · Feb 18, 2014 at 02:39 PM · c#listinventorycontains

Inventory Script. List Contains. I dont know the need bit of code.[UNSOLVED]

I dont know the needed code for the script to check if the Players inventory has a Iron Ingot from line...

  if (PlayerInventory.Inventory.Contains(_worldItemProperties))

_WorldItemProperties is a variable holding the script WorldItemProperties

I thought it would be like this...

  if (PlayerInventory.Inventory.Contains(_worldItemProperties.ItemName ("Iron Ingot")))


I want it to check if the List (Named Inventory) has a Iron Ingot (Iron Ingot is held by a variable named ItemName in the Item class WorldItemProperties which is put into the List.)

When I do that I get...

error CS1955: The member `WorldItemProperties.ItemName' cannot be used as method or delegate

I have NO idea what that even means...I have Googled it, nothing matched my case nor did anything make sense (Thats the internet for me...)

Script with issue

 WorldItemProperties _worldItemProperties;
 void Awake(){
 
 _worldItemProperties = GetComponent<WorldItemProperties>();
 
 }
 public void InventoryWindow(int id)
 {
      if (GUI.Button (new Rect (5, 20, ButtonWidth, ButtonHeight), BronzeBar))
      {
         if (PlayerInventory.Inventory.Contains(_worldItemProperties))
         {
             PlayerInventory.Inventory.Remove (_worldItemProperties);
         }
      }
 }

WorldItemProperties

using UnityEngine; using System.Collections;

[System.Serializable] public class WorldItemProperties : MonoBehaviour {

 public string ItemName = "";
 public Texture2D InvItemTexture;
 public string ItemDescription = "";

}

PlayerInvenotory.Inventory

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlayerInventory : MonoBehaviour {
 
     private static List<WorldItemProperties> _inventory = new List<WorldItemProperties>();
     public static List<WorldItemProperties> Inventory {
                 get{ return _inventory;}
         }
 }

I have already asked on forums and they have answered in a way that makes no sense to me and left me to rot... Simple Google search's didn't do any good either.

Comment
Add comment · Show 5
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 whydoidoit · Feb 18, 2014 at 02:47 PM 0
Share

Your "if" statement in the first block of code appears to be outside a function body.

avatar image Owen-Reynolds · Feb 18, 2014 at 04:26 PM 0
Share

The short version is you're messing with stuff a few levels above what you know. The best you can do now is make "random" changes, not knowing why it works if you do fix it, and not understanding it well enough to make changes later.

"Cannot be used as a method" is a common standard program$$anonymous$$g error that means you're using () where you can't. For example, int x=7; x(3); would get the same error.

Not saying give up, but know that you're skipping a year of program$$anonymous$$g experience. Like trying to catch hackers without speaking Russian.

avatar image Ethanbf3 · Feb 19, 2014 at 04:32 PM 0
Share

@whydoidoit It is in a function, i didn't include it as it seemed irrelevant. If you must know its a OnGUI then GUI.Button

@Owen Reynolds That doesn't make sense... how would it know what its looking for without defining it?

avatar image whydoidoit · Feb 19, 2014 at 04:35 PM 0
Share

Great, thought it was probably that - but hard to tell when it's presented as a piece of code.. Where do you use ItemName - I can only see the definition

avatar image Ethanbf3 · Feb 19, 2014 at 05:09 PM 0
Share

@whydoidoit ItemName is what we are looking for. "Iron Ingot" is being held by ItemName. The List("Inventory") needs to check if it contains a _WorldItemProperties where its ItemName variabe has the value "Iron Ingot".

In the Game its just checking of the Players Inventory has a Iron Ingot item.

2 Replies

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

Answer by whydoidoit · Feb 19, 2014 at 04:53 PM

Right best bet is to use FindIndex with a predicate:

 var index = PlayerInventory.Inventory.FindIndex(item=>item.ItemName == "Iron Ingot");
 if(index >= 0)
      PlayerInventory.Inventory.RemoveAt(index);
Comment
Add comment · Show 3 · 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 Ethanbf3 · Feb 19, 2014 at 05:49 PM 0
Share

@whydoidoit Ah, it seems to work! :)

Never seen this sort of code before, going to need to look into it to understand it. Cheers!

avatar image whydoidoit · Feb 20, 2014 at 01:29 AM 0
Share

No problem - look for closures and lambda syntax in .NET stuff. There's stuff on it over on http://unitygems.com

You should also check out Linq (again article on Gems)

avatar image whydoidoit · Feb 20, 2014 at 01:30 AM 0
Share

Oh and could you tick the answer please :)

avatar image
0

Answer by deltamish · Feb 18, 2014 at 02:41 PM

Try using

 if(_worldItemProperties.ItemName == "Iron Ingot")
 {
  //
 }


rather tthan

 _worldItemProperties.ItemName ("Iron Ingot")enter code here


EDIT

Try this .You should never check if an string is eaual to another string inside of Contains method.Intead use this piece of code

  if (PlayerInventory.Inventory.Contains(_worldItemProperties))
 {
  int index = PlayerInventory.Inventory.IndexOf(_worldItemProperties);
 
 if(PlayerInventory.Inventory[index].ItemName == "Iron Ingot")
 {
 // Do something fun if the above statement is satisfied 
 }
 }
Comment
Add comment · Show 6 · 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 Ethanbf3 · Feb 18, 2014 at 02:50 PM 0
Share

Nope, didnt work...

I did try...(I assumed you meant this...) [CODE] if (PlayerInventory.Inventory.Contains(_worldItemProperties.ItemName == "Iron Ingot")) [/CODE]

and in return I got...

[QUOTE]error CS1502: The best overloaded method match for System.Collections.Generic.List.Contains(WorldItemProperties)' has some invalid arguments [/QUOTE] [QUOTE] error CS1503: Argument #1' cannot convert bool' expression to type WorldItemProperties' [/QUOTE]

I also tried it like so... [CODE] if(_worldItemProperties.ItemName == "Iron Ingot") [/CODE]

then I got the Null error

avatar image whydoidoit · Feb 18, 2014 at 03:02 PM 0
Share

Can you confirm if the code you have posted is correct and you have the if statement outside the body of the Awake?

avatar image deltamish · Feb 18, 2014 at 03:13 PM 0
Share

I did try...(I assumed you meant this...) [CODE] if (PlayerInventory.Inventory.Contains(_worldItemProperties.ItemName == "Iron Ingot")) [/CODE]

You cant equate a string in Contains function I will rpost my answer in a while check it out

avatar image whydoidoit · Feb 18, 2014 at 03:15 PM 2
Share

It should be like this:

 WorldItemProperties _worldItemProperties;
 void Awake(){
  
     _worldItemProperties = GetComponent<WorldItemProperties>();
  
 
     if (PlayerInventory.Inventory.Contains(_worldItemProperties))
     {
         PlayerInventory.Inventory.Remove (_worldItemProperties);
     }
 }
avatar image Ethanbf3 · Feb 19, 2014 at 04:39 PM 0
Share

@deltamish Nope didnt get the desired result on the edit.

@whydoidoit Yes, it is confirmed that the if statement is outside the Awake function. It is under a GUI.Button which is under a OnGUI function.

In response to your solution...s Thats the exact same code as I posted, also didnt get the desired result.

@Everyone (Everyone meaning all posters) I will be updating the question shortly to fill any gaps and hopefully make the issue clearer. :)

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Add random amount of random items from one list to another? 2 Answers

Delete first object of the same type in a list? C# 1 Answer

How to ignore base class? 1 Answer

check if list contains item with matching string property 2 Answers

Lists as Parameters? 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