Search Unity

Errors CS0176, CS1503 CS1502

Discussion in 'Scripting' started by Ethanbf3, Feb 9, 2014.

  1. Ethanbf3

    Ethanbf3

    Joined:
    Feb 15, 2012
    Posts:
    153
    I have no idea what these errors mean and PLEASE don't criticize me for how "easy" these are, im not in the mood...:'(

    All on line 12

    W hile hovering over the 'Add' function, Monodevelop states "Unknown Resolve Error"

    This is a Inventory system and im trying to add a item to the inventory when the stated events happen.

    Code (csharp):
    1.    
    2. //-----------------------------Interaction Script----------------------------------
    3. void ObtainItem(){
    4.  
    5.         _playerInventory = GetComponent<PlayerInventory>();
    6.  
    7.         if (_worldItemProperties.ItemQuantity > 1)
    8.         {
    9.             Interaction.text = "Obtained " + _worldItemProperties.ItemQuantity + _worldItemProperties.ItemName + "s!";
    10.             Debug.Log ("Obtained " + _worldItemProperties.ItemQuantity + " " + _worldItemProperties.ItemName + "s!");
    11.             if (_worldItemProperties.Respawn)
    12.             {
    13.                 _playerInventory.Inventory.Add(_worldItemProperties.ItemName); //This line adds a item when player presses a button
    14.                 _worldItemProperties.gameObject.SetActive (false);
    15.                         }
    16.                }
    17. }
    18.  
    19. //-----------------------------------WorldItemPropertiesScript--------------------------
    20.     public string ItemName = "Cookies";
    21.  
    22. //-------------------------------PlayerInventory-------------------------------------
    23. public class PlayerInventory : MonoBehaviour {
    24.  
    25.     //BergZerg Arcade Inventory tutorial 1 has been followed for the following result
    26.     private static List<WorldItemProperties> _inventory = new List<WorldItemProperties>();
    27.     public static List<WorldItemProperties> Inventory {
    28.                 get{ return _inventory;}
    29.         }
    30. }
    31.  
    Please State if the following errors were easy or not and how you knew how to fix, im desperate...
     
  2. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    error CS0176: Static member `PlayerInventory.Inventory' cannot be accessed with an instance reference, qualify it with a type name instead

    Like the message says, the "Inventory" member of the PlayerInventory class has been declared as static here:

    Code (csharp):
    1.  
    2. public static List<WorldItemProperties> Inventory {
    3.  
    That basically means that it belongs to the class itself, not to an instance of the class. Since _playerInventory is an *instance* of the PlayerInventory class, you can't use it to access the Inventory member. Instead, you need to reference it like:

    Code (csharp):
    1.  
    2. PlayerInventory.Inventory
    3. // note the *class* name is being used here, not an *instance* name
    4.  
    The CS1502 and CS1503 errors are both caused by the same issue. That is, the Inventory member is defined as a List of WorldItemProperties here:


    Code (csharp):
    1.  
    2. public static List<WorldItemProperties> Inventory {
    3.  
    So, you can only "Add" a WorldItemProperty to Inventory. That's not what you're doing here:

    Code (csharp):
    1.  
    2. _playerInventory.Inventory.Add(_worldItemProperties.ItemName);
    3.  
    You're trying to add an "ItemName" instead, which according to the error is a *string*. That's not going to work...

    Jeff