I am working on setting up a simple singleton instance of an inventory script that can be accessed by an item pickup script, and am getting a confusing error. When I go to call the Inventory singleton, I get a compiler error that says ‘Inventory’ does not contain a definition for ‘instance’.
But it does contain this definition, it is public static, it auto-completes, and in Visual Studio I can “go to declaration” and it takes me there.
Here is my inventory script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public static Inventory instance;
public List<Item> items = new List<Item>();
void Awake()
{
if (instance != null)
{
Debug.LogWarning("More than one instance of inventory found");
return;
}
instance = this;
}
public bool Add(Item item)
{
items.Add(item);
return true;
}
}
And here is the code for the ItemPickUp:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemPickUp : MonoBehaviour
{
// This script is for items that you pick up
public Item item;
void PickUp()
{
Debug.Log("Picking up" + item.name);
bool wasPickedUp = Inventory.instance.Add(item);
if (wasPickedUp)
{
Destroy(gameObject);
}
}
}
The specific error is: Assets/Scripts/ItemPickUp.cs(26,38): error CS0117: ‘Inventory’ does not contain a definition for ‘instance’
I feel like I am overlooking something dumb. Any ideas?
It is a Unity error, and yes I have confirmed that I’ve saved both files. Also made sure they were in the same correct scripts folder. Thanks for the suggestion though.
I have found another clue…I’ve put a simple Debug.Log(“Hello world”) in the Awake() function of the Inventory.cs script…and it does not print! For some reason this script doesn’t seem to be active at all…?
You’ll need to make sure that it’s compiled in Unity. When you return to Unity, normally you’ll see it recompile. If not, then you can force a recompile. When you click on the script in Unity and it gives you a preview in the inspector, make sure you see the script as you have it written above.
If you go under preferences in Unity, also check that your External Script Editor is setup for Visual Studio.(or whatever editor you use)
Your debug.log wouldn’t print if you’re having a compile error because Unity shouldn’t even run. Unless you removed the call to the Inventory.instance.(which you mentioned is the source of your error) If your awake and start methods don’t run on a script, just verify it’s in the scene on an active gameobject.
This is definitely the problem! My Inventory.cs script does not change in the Unity Inspector, despite changes in Visual Studio. However, all my other .cs files do change in the Unity Inspector. Something wrong with this one file. It is in the same folder as the others, and when I double click in Unity, it takes me to the right file in Visual Studio…
This is strange, I thought Unity recompiled all files every time.
I tried CTRL + R, also re-importing the asset. Nothing worked. Eventually I tried quitting Visual Studio and reopening, and the Inventory.cs file was reverted back to the old version that was being showed in the inspector! So for some reason Visual Studio just wasn’t saving the file, even though it was showing as saved in Visual Studio.
Very weird…I’m not sure what to make of it. But it’s working now.
Thank you so much spiney199 and Brathnann for all your help!