Error CS0103 The name `i' does not exist in the current context
I have error on this script. Help
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class ItemDatabase : MonoBehaviour { public List InventoryDat = new List ();
void FixedUpdate()
{
foreach (Item i in InventoryDat) {
{
i.itemAmount = PlayerPrefs.GetInt (i.itemName);
}
if (i.itemAmount > 0) {
i.itemSlot.SetActive (true);
} else
i.itemSlot.SetActive (false);
}
if(i.itemAmount != PlayerPrefs.GetInt(i.itemName))
{
PlayerPrefs.SetInt(i.itemName, i.itemAmount);
print ("Saved:" + i.itemName);
}
}
}
Comment
Answer by Cuttlas-U · Apr 11, 2017 at 09:12 AM
Hi; u are closing the foreach in above lane 25 ; so the next lanes of the code wont recognize i variable caouse it was defiend in the foreach loop;
so this may solve your problem i think :
void FixedUpdate()
{
foreach (Item i in InventoryDat)
{
{
i.itemAmount = PlayerPrefs.GetInt(i.itemName);
}
if (i.itemAmount > 0)
{
i.itemSlot.SetActive(true);
}
else
i.itemSlot.SetActive(false);
if (i.itemAmount != PlayerPrefs.GetInt(i.itemName))
{
PlayerPrefs.SetInt(i.itemName, i.itemAmount);
print("Saved:" + i.itemName);
}
}
}