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
{
}
}