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

Compilation Errors for Database Scripts

Discussion in 'Scripting' started by drl92, Mar 6, 2021.

  1. drl92

    drl92

    Joined:
    Feb 6, 2021
    Posts:
    7
    I'm trying to make an item database so I followed an online guide however, the scripts outlined in the guide have compilation errors. Any help would be appreciated, I'm a beginner in both Unity and C# so please make the explanations simple. The errors indicated in unity are as follows:


    1. item.cs
    "There are inconsistent line endings in the 'Assets/Inventory/Scripts/item.cs' script. Some are Mac OS X (UNIX) and some are Windows.
    This might lead to incorrect line numbers in stacktraces and compiler errors. Many text editors can fix this using Convert Line Endings menu commands." -I tried converting the line endings but the error remained.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu(fileName = "New Item", menuName = "Assets/Item")]
    4. public class Item : ScriptableObject
    5. {
    6.     public string itemID;
    7.     public string itemName;
    8.     [TextArea]
    9.     public string itemDescription;
    10.     public int itemCost;
    11.     public Sprite itemSprite;
    12. }
    2. ItemDataBase.cs
    "There are inconsistent line endings in the 'Assets/Inventory/Scripts/ItemDataBase.cs' script. Some are Mac OS X (UNIX) and some are Windows.
    This might lead to incorrect line numbers in stacktraces and compiler errors. Many text editors can fix this using Convert Line Endings menu commands."
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(fileName = "New Item Database" , menuName = "Assets/Databases/Item Database")]
    6. public class ItemDatabase : ScriptableObject
    7. {
    8.     public List<Item> allItems;
    9. }
    3.DataBase.cs
    "Assets\Inventory\Scripts\Database.cs(30,40): error CS0119: 'Database.GetRandomItem()' is a method, which is not valid in the given context"
    "Assets\Inventory\Scripts\Database.cs(30,40): error CS0119: 'Database.GetRandomItem()' is a method, which is not valid in the given context
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6. public class Database : MonoBehaviour
    7. {
    8.     public ItemDatabase items;
    9.     private static Database instance;
    10.  
    11.     private void Awake()
    12.     {
    13.         if(instance == null)
    14.         {
    15.             instance = this;
    16.             DontDestroyOnLoad(gameObject);
    17.         }
    18.         else
    19.         {
    20.             Destroy(gameObject);
    21.         }
    22.     }
    23.  
    24.     public static Item GetItemByID(string ID)
    25.     {
    26.         return instance.items.allItems.FirstOrDefault(i => i.itemID == ID);
    27.     }
    28.     public static Item GetRandomItem()
    29.     {
    30.         return instance.items.allItems[GetRandomItem.Range(0, instance.items.allItems.count())];
    31.     }
    32.  
    33.  
    34. }
    "
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Inconsistent line endings is just a warning. It's also pretty self explanatory. I won't elaborate further.

    I think you meant Random.Range probably instead of GetRandomItem.Range?

    Also
    count()
    is incorrect. It's just a property and it's
    Count
    , capitalized and no parentheses.
     
    Joe-Censored likes this.
  3. drl92

    drl92

    Joined:
    Feb 6, 2021
    Posts:
    7
    Thank you for the help, the comp errors are resolved now.