Ok, I understand this error, and I know the answer is simple: "Declare a static variable".
But my problem is: I want to use a simple public variable, so that I can drag and drop the GameObjects onto them in the inspector, which is impossible using a static variable.
The error says: "An object reference is required to access non-static member", what is this "object reference"? And how can I use it?
Here is the simplified version of the code:
public class InventorySystem : MonoBehaviour
{
public static GameObject[] inventory;
public GameObject[] inventorySlot;
public Material item_Key, item_Cube, item_Sphere;
void Start ()
{
inventory = new GameObject[slots];
}
void Update()
{
...
}
public static void SortMenu()
{
for(int i = 0; i < slots; i++)
{
switch(inventory[i].name)
{
case "Cube1":
inventorySlot[i].renderer.material = item_Cube;
break;
case "Cube2":
inventorySlot[i].renderer.material = item_Cube;
break;
case "Sphere":
inventorySlot[i].renderer.material = item_Sphere;
break;
case "Sphere2":
inventorySlot[i].renderer.material = item_Sphere;
break;
}
}
}
}
And the exact error I'm getting is: "An object reference is required to access non-static member `InventorySystem.inventorySlot'", inside the switch estatement on the lines containing "inventorySlot[i].renderer.material".
I know I can use Find, but I really want to avoid using it, although, if there's no option I'd have no other choice, but I want to understand more this error.
Thank you.
↧