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. Dismiss Notice

Question Can't add script

Discussion in 'Scripting' started by unity_jjhtpWoQZZx2Hw, Dec 20, 2020.

  1. unity_jjhtpWoQZZx2Hw

    unity_jjhtpWoQZZx2Hw

    Joined:
    Oct 30, 2020
    Posts:
    9
    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:

    Bildschirmfoto 2020-12-20 um 09.39.19.png
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour {
    6.    
    7.     public ItemDatabase database;
    8.    
    9.     int slotAmount = 9;
    10.     int storageAmount = 36;
    11.    
    12.     public GameObject slot;
    13.     public GameObject hotbarPanel;
    14.     public GameObject inventoryPanel;
    15.    
    16.     // Start is called before the first frame update
    17.     void Start() {
    18.         database = gameObject.GetComponent<ItemDatabase>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update() {
    23.        
    24.     }
    25. }
    26.  
    ItemDatabase.cs:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6. [System.Serializable]
    7. public struct Item {
    8.     public int ID;
    9.     public string Name;
    10.     public bool Stackable;
    11.     public string Slug;
    12.    
    13.     public Item (int id, string name, bool stackable, string slug) {
    14.         ID = id;
    15.         Name = name;
    16.         Stackable = stackable;
    17.         Slug = slug;
    18.     }
    19.  
    20. public class ItemDatabase : MonoBehaviour {
    21.  
    22.     public List<Item> itemDatabase = new List<Item>();
    23.     // Start is called before the first frame update
    24.     void Start() {
    25.         GetDatabase("Assets/Ressources/ItemData.rtf");
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update() {
    30.        
    31.     }
    32.    
    33.     void GetDatabase(string path) {
    34.         StreamReader sr = new StreamReader(path);
    35.        
    36.         AddItem:
    37.         itemDatabase.Add(new Item(
    38.             int.Parse(sr.ReadLine().Replace("id: ", "")),
    39.             sr.ReadLine().Replace("name: ", ""),
    40.             bool.Parse(sr.ReadLine().Replace("stackable: ", "")),
    41.             sr.ReadLine().Replace("slug: ", "")
    42.             ));
    43.            
    44.             string c = sr.ReadLine();
    45.             if(c == ",") {
    46.                 goto AddItem;
    47.             } else if(c == ",") {
    48.                 sr.Close();
    49.             } else {
    50.                 Debug.LogError("ItemData doesn't have correct line ending");
    51.             }
    52.         }
    53.     }
    54. }
    55.  
    Sincerely yours,
    Atten007
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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
     
    unity_jjhtpWoQZZx2Hw likes this.
  3. unity_jjhtpWoQZZx2Hw

    unity_jjhtpWoQZZx2Hw

    Joined:
    Oct 30, 2020
    Posts:
    9
    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
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
  5. unity_jjhtpWoQZZx2Hw

    unity_jjhtpWoQZZx2Hw

    Joined:
    Oct 30, 2020
    Posts:
    9
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6. [System.Serializable]
    7. public struct Item {
    8.     public int ID;
    9.     public string Name;
    10.     public bool Stackable;
    11.     public string Slug;
    12.    
    13.     public Item (int id, string name, bool stackable, string slug) {
    14.         ID = id;
    15.         Name = name;
    16.         Stackable = stackable;
    17.         Slug = slug;
    18.     }
    19.  
    20. public class ItemDatabase : MonoBehaviour {
    21.  
    22.     public List<Item> itemDatabase = new List<Item>();
    23.     // Start is called before the first frame update
    24.     void Start() {
    25.         GetDatabase("Assets/Ressources/ItemData.rtf");
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update() {
    30.        
    31.     }
    32.    
    33.     void GetDatabase(string path) {
    34.         StreamReader sr = new StreamReader(path);
    35.        
    36.         AddItem:
    37.         itemDatabase.Add(new Item(
    38.             int.Parse(sr.ReadLine().Replace("id: ", "")),
    39.             sr.ReadLine().Replace("name: ", ""),
    40.             bool.Parse(sr.ReadLine().Replace("stackable: ", "")),
    41.             sr.ReadLine().Replace("slug: ", "")
    42.             ));
    43.            
    44.             string c = sr.ReadLine();
    45.             if(c == ",") {
    46.                 goto AddItem;
    47.             } else if(c == ",") {
    48.                 sr.Close();
    49.             } else {
    50.                 Debug.LogError("ItemData doesn't have correct line ending");
    51.             }
    52.         }
    53.     }
    54. }
    55.  
    Sincerely yours,
    Atten007
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Still the same issue. I've already posted what to do: