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.
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.
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!
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.
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.
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
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.
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
Follow this Question
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