Can't add script

Hi folks,

I’m currently programming my own Minecraft (it’s name is Mikacraft). In the video the programmer dragged a script to the game object “Inventory Manager”, but in my Unity I CAN’T drag the both scripts to the game object. Here’s an image of the note:


What I’ve made wrong here? I can’t drag the scripts, so my game doesn’t work! If you can help me, it’ll be really nice!

Here’s the code of the scripts:
Inventory.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour {
   
    public ItemDatabase database;
   
    int slotAmount = 9;
    int storageAmount = 36;
   
    public GameObject slot;
    public GameObject hotbarPanel;
    public GameObject inventoryPanel;
   
    // Start is called before the first frame update
    void Start() {
        database = gameObject.GetComponent<ItemDatabase>();
    }

    // Update is called once per frame
    void Update() {
       
    }
}

ItemDatabase.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

[System.Serializable]
public struct Item {
    public int ID;
    public string Name;
    public bool Stackable;
    public string Slug;
   
    public Item (int id, string name, bool stackable, string slug) {
        ID = id;
        Name = name;
        Stackable = stackable;
        Slug = slug;
    }

public class ItemDatabase : MonoBehaviour {

    public List<Item> itemDatabase = new List<Item>();
    // Start is called before the first frame update
    void Start() {
        GetDatabase("Assets/Ressources/ItemData.rtf");
    }

    // Update is called once per frame
    void Update() {
       
    }
   
    void GetDatabase(string path) {
        StreamReader sr = new StreamReader(path);
       
        AddItem:
        itemDatabase.Add(new Item(
            int.Parse(sr.ReadLine().Replace("id: ", "")),
            sr.ReadLine().Replace("name: ", ""),
            bool.Parse(sr.ReadLine().Replace("stackable: ", "")),
            sr.ReadLine().Replace("slug: ", "")
            ));
           
            string c = sr.ReadLine();
            if(c == ",") {
                goto AddItem;
            } else if(c == ",") {
                sr.Close();
            } else {
                Debug.LogError("ItemData doesn't have correct line ending");
            }
        }
    }
}

Sincerely yours,
Atten007

It doesn’t recognize your class ‘ItemDatabase’ because it is nested inside (i.e. declared within the scope) of your ‘Item’ struct, so the compiler doesn’t know ‘ItemDatabase’, but ‘Item.ItemDatabase’.
I assume that’s just a mistake on your side.

It’ll be the best if you do not nest these types, and just remove the curly bracket at the bottom, and insert one at the end of your ‘Item’ struct to close it properly before your ‘ItemDatabase’ starts. Or just move them to separate files, so that won’t happen again.

Also, if that’s the original code from the tutorial you’re working through, I strongly recommend to look for a better tutorial ASAP, because it appears to be awful when it comes to coding practices:

  1. goto & label - don’t do that even if it works, a loop is what you’re looking for
  2. very unsafe and unreliable use of stream objects
  3. and poor handling of file content
1 Like

Hi Suddoha,

Now my game does work, I had to complete “Item.” to ItemDatabase. Thanks for your help (that’s the reason why I’ve left you a like), but at the end of the file, there’re two curly brackets, which one should I remove? The curly bracket in line 24 or the curly bracket in line 25? And I can’t add the script ItemDatabase to the InventoryManager, what should I complete/correct? I ask you this because I only know a bit C#.

Sincerely yours,
Atten007

Technically you already fixed it, so you do not need to change the curly brackets. But that solution is not the one you’re looking for because your ItemDatabase is a MonoBehaviour, and that’s better not to be nested in another type.

So yeah, you need to fix the curly brackets instead, which - at the same time - means you won’t need to use Item.ItemDatabase because the ItemDatabase will no longer be nested in the Item struct. Then, you can just use ItemDatabase directly.

It doesn’t matter which one you remove if there’s nothing in between them. However, the lines you mentioned do not match the lines in your original post, so let’s just talk about the code in your original post. Just delete line 54, and add that bracket in line 19. Now the brackets should be placed properly and ItemDatabase can be used as you originally intended. Also make sure ItemDatabase resides in a file that’s called ItemDatabase.cs.

Hi Suddoha,

Hi Suddoha,

I was now able to drag the inventory script to the inventory manager, but it still doesn’t work with the ItemDatabase script. What do I have to change in the script? Here’s the source code of the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

[System.Serializable]
public struct Item {
    public int ID;
    public string Name;
    public bool Stackable;
    public string Slug;
   
    public Item (int id, string name, bool stackable, string slug) {
        ID = id;
        Name = name;
        Stackable = stackable;
        Slug = slug;
    }

public class ItemDatabase : MonoBehaviour {

    public List<Item> itemDatabase = new List<Item>();
    // Start is called before the first frame update
    void Start() {
        GetDatabase("Assets/Ressources/ItemData.rtf");
    }

    // Update is called once per frame
    void Update() {
       
    }
   
    void GetDatabase(string path) {
        StreamReader sr = new StreamReader(path);
       
        AddItem:
        itemDatabase.Add(new Item(
            int.Parse(sr.ReadLine().Replace("id: ", "")),
            sr.ReadLine().Replace("name: ", ""),
            bool.Parse(sr.ReadLine().Replace("stackable: ", "")),
            sr.ReadLine().Replace("slug: ", "")
            ));
           
            string c = sr.ReadLine();
            if(c == ",") {
                goto AddItem;
            } else if(c == ",") {
                sr.Close();
            } else {
                Debug.LogError("ItemData doesn't have correct line ending");
            }
        }
    }
}

Sincerely yours,
Atten007

Still the same issue. I’ve already posted what to do: