- Home /
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.
Your "if" statement in the first block of code appears to be outside a function body.
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.
@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?
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
@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.
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);
@whydoidoit Ah, it seems to work! :)
Never seen this sort of code before, going to need to look into it to understand it. Cheers!
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)
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
}
}
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
Can you confirm if the code you have posted is correct and you have the if statement outside the body of the Awake?
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
It should be like this:
WorldItemProperties _worldItemProperties;
void Awake(){
_worldItemProperties = GetComponent<WorldItemProperties>();
if (PlayerInventory.Inventory.Contains(_worldItemProperties))
{
PlayerInventory.Inventory.Remove (_worldItemProperties);
}
}
@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. :)
Your answer
Follow this Question
Related Questions
generic list, swapping equipment errors but not always? 1 Answer
How can I remove an item ffrom a list. 1 Answer
Count object list duplicates 1 Answer
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer