0

This is the Item class

//Hakeem Thomas
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item : MonoBehaviour
{
    [SerializeField] protected string name;
    [SerializeField] protected int quantity;

    void start()
    {
        quantity = 0;
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Quantity
    {
        get { return quantity; }
        set { quantity = value; }
    }

    public virtual void UseItem()
    {

    }
}

This is part of a script that I'm attaching to a game object and referencing it to be picked up by the player.

private void OnTriggerEnter(Collider other)
    {
        Item temp = this.GetComponent<Item>();
        if (Inventory.invItems.Count < 12)
        {
            if (other.gameObject.CompareTag("Player"))
            {
                Inventory.AddToInventory(temp);
                h.SetActive(false);
            }
        }
    }

The script producing the following error:

Severity Code Description Project File Line Suppression State Suppression State Error CS1503 Argument 1: cannot convert from 'UnityEngine.GameObject' to 'Item' Assembly-CSharp C:\Users\Owner\Downloads\Project 3 (1)\Project 3\Assets\Scripts\Weapons.cs 63 Active

This is the method to add Items to my linkedlist

//Hakeem Thomas
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//(Application.persistentDataPath + "/example.txt");
public class Inventory : MonoBehaviour
{
    public GUI_2D popUp;
    HealthItems hpPo;

    public static LinkedList<Item> invItems;
    //private Item[] quickItems;
    string[] str;
    int Qty;
    public int InvSize;

    void Start()
    {

    }

    public void InitializeInventory()
    {
        invItems = new LinkedList<Item>();
        InvSize = invItems.Count;
        str = new string[12];
    }

    public void AddToInventory( Item newItem)
    {
        bool found = false;
        if (invItems.Count < 12)
        {

            foreach (Item x in invItems)
            {
                if (x.Name == newItem.name)
                {
                    x.Quantity++;
                    found = true;
                    break;
                }
            }
        }
        else
        {
            Debug.Log("the storage is full and cannot take any more");
        }
        if (!found && invItems.Count < 12)
        {
            invItems.AddLast(newItem);
        }
    }


    void OnGUI()
    {
        if (popUp.menu)
        {
            GUILayout.BeginArea(new Rect(50, 100, 250, 400), "Inventory");
            GUILayout.Space(20);
            int c = 0;
            if (invItems.Count > 0)
            {
                foreach (Item x in invItems)
                {
                    c++;
                    if (GUILayout.Button(x.Name + " " + x.Quantity))
                    {
                        // str[invItems.Count] = x.Name;
                        x.UseItem();
                        if (x.Quantity > 1)
                        {
                            x.Quantity--;
                        }
                        else
                        {
                            invItems.Remove(x);
                        }

                    }

                }
                if (c > 6) { c = 6; }
            }


            for (int i = 0; i < 6 - c; i++)
                if (GUILayout.Button("null " + " " + 0)) { }

            GUILayout.EndArea();

        }
    }
}




What I am trying to do is allow my objects when picked up to be added to my inventory. My Listedlist is required. I don't know how to fix this issue.

I also have one more method that doesn't give errors but I can't implement it correctly:

void OnTriggerEnter(Collider other)
    {
        Item temp = other.GetComponent<Item>();
        if (other.gameObject.CompareTag("Shotgun"))
        {
            add.AddToInventory(temp);
            other.gameObject.SetActive(false);
            wep.shotGun = true;
        }
        else if (other.gameObject.CompareTag("Pellet"))
        {
            add.AddToInventory(temp);
            other.gameObject.SetActive(false);
            wep.pellet = true;
        }
        else if (other.gameObject.CompareTag("Bargun"))
        {
            add.AddToInventory(temp);
            other.gameObject.SetActive(false);
            wep.barGun = true;
        }
        else
        {

        }
    }
Hakeem Thomas
  • 123
  • 1
  • 9
  • Which line of code is causing the error? (What's line 63 of Weapon.cs?) – Matt U Dec 07 '19 at 03:06
  • Which line of code is 63 in the code you shared? – Matt U Dec 07 '19 at 03:10
  • I made a mistake. There are two codes that are the same. the error is actually: >Severity Code Description Project File Line Suppression State Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'Inventory.AddToInventory(Item)' Assembly-CSharp C:\Users\Owner\Downloads\Project 3 (1)\Project 3\Assets\Scripts\HealthItems.cs 41 Active – Hakeem Thomas Dec 07 '19 at 03:12
  • @HakeemThomas Could you share your code for `Inventory`? – WQYeo Dec 07 '19 at 03:13
  • How do I share code on StackOverflow? – Hakeem Thomas Dec 07 '19 at 03:13
  • @HakeemThomas just like you have posted the question. Edit the question and add the code – Anu Viswan Dec 07 '19 at 03:14

1 Answers1

0

The problem lies in calling your properties from the Inventory class:

private void OnTriggerEnter(Collider other)
    {
        Item temp = this.GetComponent<Item>();
        // This
        if (Inventory.invItems.Count < 12)
        {
            if (other.gameObject.CompareTag("Player"))
            {
                // This
                Inventory.AddToInventory(temp);
                h.SetActive(false);
            }
        }
    }

Notice that you did Inventory.invItems and Inventory.AddToInventory(), which treats it as calling a static field.
(It happens when you access a property from the class's name, rather than an instance.)

However, none of that property is static, which results in that error.
This means that you will need a reference to an Inventory object in order to use those properties.

You might want to either make those properties static, or use a singleton instead.

Good Reads:

WQYeo
  • 3,973
  • 2
  • 17
  • 26