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 /
  • Help Room /
avatar image
0
Question by AdvsNoob · Jun 27, 2017 at 02:38 PM · scripting problemprogramminglistsloops

Cant loop through a List

Here is the code in question:

 public List<Item> craftingItems = new List<Item>();
 
 public void checkIfItemIsCraftable(){
         for (int i = 0; i < inv.database.craftingDatabase.Count; i++) {
             for (int x = 0; i < craftingItems.Count; x++) {
                 if (craftingItems [x].ID == inv.database.craftingDatabase [i] ["craft"] ["itemID_1"] || craftingItems [x].ID == inv.database.craftingDatabase [i] ["craft"] ["itemID_2"]) {
                     if (craftingItems [x].ID == inv.database.craftingDatabase [i] ["craft"] ["itemID_1"] || craftingItems [x].ID == inv.database.craftingDatabase [i] ["craft"] ["itemID_2"]) {
                         Debug.Log (inv.database.craftingDatabase [i].Name);
 
                     }
                 }
             }
         }
     }


Very sloppy I know but its because I'm just trying to figure something out. I am these errors:

Assets/Scripts/ItemCrafting.cs(40,68): error CS0021: Cannot apply indexing with [] to an expression of type `Item'.

Assets/Scripts/ItemCrafting.cs(41,67): error CS0021: Cannot apply indexing with [] to an expression of type `Item'.

I have looped through many variations of the the same type of List before but for some reason this one will not work. I have no idea if I am overlooking something but its gotten to the point were I just can't figure it out. If someone could shine some light on this I would greatly appreciate it!

Thank you.

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

2 Replies

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

Answer by AdvsNoob · Jun 28, 2017 at 07:08 AM

I figured it out, idk why I missed it maybe it was because I was up for so long.

Comment
Add comment · Show 2 · 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 nathanlink169 · Jun 28, 2017 at 10:49 AM 0
Share

Hi AdvsNoob - can you post your solution? Others will find this answer and if they are having the same issue, could be frustrated that they can't find the answer.

Congratulations on finding the error!

avatar image AdvsNoob nathanlink169 · Jun 28, 2017 at 11:46 AM 0
Share

The solution was simple I was doing

inv.database.craftingDatabase [i] ["craft"] ["itemID_1"]

when it should of been

inv.database.craftingDatabase[i].ItemId1

Error on my part.

avatar image
0

Answer by nathanlink169 · Jun 27, 2017 at 10:18 PM

There should be an underline, depending on your IDE, marking the exact issue - but that's neither here nor there.

The only issue that I can see here lies in inv.database.craftingDatabase: Can you access an array 3 times in this situation? Meaning, whatever craftingDatabase[i] is equal to, can you use the [] accessor on it? Can you use that accessor or the next one down? And the next? - Your craftingItems[x] code seems to be fine.

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 AdvsNoob · Jun 28, 2017 at 02:47 AM 0
Share

public List craftingDatabase = new List ();

and I'm its copying this

private List database = new List();

by doing this: craftingDatabase = database;

I am so stumped right now

avatar image nathanlink169 AdvsNoob · Jun 28, 2017 at 03:07 AM 0
Share

Hi AdvsNoob, could you please attach the entire file, either to myself in a private message, or to this post, so that I can look at it a bit more in depth? Thanks.

avatar image AdvsNoob nathanlink169 · Jun 28, 2017 at 06:05 AM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ItemCrafting : $$anonymous$$onoBehaviour {

 Inventory inv;

 public Button inventoryCraftingButton;

 public List<Item> craftingItems = new List<Item>();

 // Use this for initialization
 void Start () {
     inv = this.GetComponent<Inventory> ();
     
 }

 public void TogleCrafting(){
     inv.isCrafting = !inv.isCrafting;
     if (inv.isCrafting) {
         Debug.Log ("Crafting");
         inventoryCraftingButton.GetComponent<Image> ().sprite = inv.buttonPressed;


     } else {
         Debug.Log ("Crafting Stopped");
         inventoryCraftingButton.GetComponent<Image> ().sprite = inv.buttonUnPressed;
     }
 }

 public void CraftItems(){
     checkIfItemIsCraftable ();
 }

 public void checkIfItemIsCraftable(){
     for (int i = 0; i < inv.database.craftingDatabase.Count; i++) {
         for (int x = 0; i < craftingItems.Count; x++) {
             if (craftingItems [x].ID == inv.database.craftingDatabase [i] ["craft"] ["itemID_1"] || craftingItems [x].ID == inv.database.craftingDatabase [i] ["craft"] ["itemID_2"]) {
                 if (craftingItems [x].ID == inv.database.craftingDatabase [i] ["craft"] ["itemID_1"] || craftingItems [x].ID == inv.database.craftingDatabase [i] ["craft"] ["itemID_2"]) {
                     Debug.Log (inv.database.craftingDatabase [i].Name);

                 }
             }
         }
     }
 }

}

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

148 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 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 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

Help to Add some Sort and Binary Search in the code. 1 Answer

Velocity in Player Movement Problem? 0 Answers

Namespce Error!!!!!!!!! 0 Answers

.rotation doesn't seem to work, can't shoot projectile at mouse coordinates 0 Answers

VR Object Snapping 0 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