Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Misthra-Games · Aug 23, 2016 at 02:21 AM · gameobjectlistgameobjectslistsforeach

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);
 
     }
 
 
 }
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
0
Best Answer

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.

Comment
Add comment · Show 8 · 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 Misthra-Games · Aug 23, 2016 at 06:06 AM 0
Share

I did what you said and got this erroralt text

error.png (3.0 kB)
avatar image JedBeryll Misthra-Games · Aug 23, 2016 at 06:15 AM 0
Share

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;
  }

avatar image Misthra-Games JedBeryll · Aug 23, 2016 at 06:52 AM 0
Share

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()

Show more comments
avatar image NoseKills Misthra-Games · Aug 23, 2016 at 06:19 AM 0
Share

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;
             }
         }
  }
avatar image Misthra-Games NoseKills · Aug 23, 2016 at 06:34 AM 0
Share

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

Show more comments
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.

Follow this Question

Answers Answers and Comments

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

Related Questions

C# List foreach problem 1 Answer

For loop does not loop C# 2 Answers

Keep list of GameObjects between scenes 2 Answers

Setting a game object from a list to be the active character that gets instantiated 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers


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