Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Singleton instance not found, even though autocompletes, and can goto definition?

Discussion in 'Scripting' started by bricemorrison, Feb 16, 2022.

  1. bricemorrison

    bricemorrison

    Joined:
    Dec 18, 2018
    Posts:
    6
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.     public static Inventory instance;
    8.  
    9.     public List<Item> items = new List<Item>();
    10.  
    11.     void Awake()
    12.     {
    13.         if (instance != null)
    14.         {
    15.             Debug.LogWarning("More than one instance of inventory found");
    16.             return;
    17.         }
    18.  
    19.         instance = this;
    20.     }
    21.  
    22.     public bool Add(Item item)
    23.     {
    24.         items.Add(item);
    25.         return true;
    26.     }
    27.  
    28. }
    And here is the code for the ItemPickUp:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ItemPickUp : MonoBehaviour
    6. {
    7.     // This script is for items that you pick up
    8.  
    9.     public Item item;
    10.  
    11.     void PickUp()
    12.     {
    13.         Debug.Log("Picking up" + item.name);
    14.         bool wasPickedUp = Inventory.instance.Add(item);
    15.  
    16.         if (wasPickedUp)
    17.         {
    18.             Destroy(gameObject);
    19.         }
    20.     }
    21. }
    22.  
    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?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    This is a Unity error yeah? Have you made sure to save both scripts?
     
  3. bricemorrison

    bricemorrison

    Joined:
    Dec 18, 2018
    Posts:
    6
    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.
     
  4. bricemorrison

    bricemorrison

    Joined:
    Dec 18, 2018
    Posts:
    6
    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...?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    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.
     
  6. bricemorrison

    bricemorrison

    Joined:
    Dec 18, 2018
    Posts:
    6
    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.

    Currently googling how to force a recompile...
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    CTRL + R is the default shortcut to get Unity to update its asset database, which will recompile any changed scripts.
     
  8. bricemorrison

    bricemorrison

    Joined:
    Dec 18, 2018
    Posts:
    6
    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!