- Home /
How to find all similar elements in a list?
I'm trying to create a crafting system but I need to know how many of each item I have it my list (aka inventory). For some reason this code is not working:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Crafting : MonoBehaviour {
private Inventory i;
public GameObject Stick;
private int sticksNum = 0;
private int stones = 0;
// Use this for initialization
void Start () {
i = gameObject.GetComponent<Inventory> ();
}
public void makeStoneAxe() {
foreach (Stick in i.getItemsInList()) {
sticksNum += 1;
}
}
// Update is called once per frame
void Update () {
Debug.Log (sticksNum);
}
}
Answer by JedBeryll · Aug 23, 2016 at 04:56 AM
foreach (Stick in i.getItemsInList()) {
sticksNum += 1;
}
the object doesn't have an identifier.
foreach (Stick s in i.getItemsInList()) {
sticksNum += 1;
}
I don't know what getItemsInList returns so i can't help with that part.
Because apparently the getItemsInList has GameObjects in it. If Stick is a component type then you need to do:
foreach (GameObject g in i.getItemsInList()) {
if (g.GetComponent<Stick>() != null) sticksNum += 1;
}
The name of the objects is stick, I just did what you said and it wroks, i get the axe in my inventory, but it gives me an error, it doesn't seem to effect anything but could you tell me what it means ?
InvalidOperationException: Collection was modified; enumeration operation may not execute. System.Collections.Generic.List`1+Enumerator[UnityEngine.GameObject].VerifyState () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:778) System.Collections.Generic.List`1+Enumerator[UnityEngine.GameObject].$$anonymous$$oveNext () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:784) Crafting.makeStoneAxe () (at Assets/Scripts/Player/Crafting.cs:15) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:144) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:621) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:756) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
Like @JedBeryll Said we don't know what i.getItemsInList()
returns. Turns out it returns a list of GameObjects but that code expects it to be a list of Sticks?
You must look for Sticks amongst them
foreach (GameObject s in i.getItemsInList()) {
if (s != null) {
var stick = s.GetComponent<Stick>();
if (stick != null) {
sticksNum += 1;
}
}
}
Nonono, it's a list of game objects and some of the game objects in the list ARE sticks, I'm trying to get it to tell me how many GameObjects in the list are sticks so i can check if there is enough sticks to craft an item