Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当游戏对象需要类引用时,如何将其添加到列表中?_C#_Unity3d - Fatal编程技术网

C# 当游戏对象需要类引用时,如何将其添加到列表中?

C# 当游戏对象需要类引用时,如何将其添加到列表中?,c#,unity3d,C#,Unity3d,这是Item类 //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()

这是Item类

//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()
    {

    }
}

这是脚本的一部分,我将其附加到游戏对象上,并引用它以供玩家拾取

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);
            }
        }
    }
专用无效对撞机(对撞机其他)
{
Item temp=this.GetComponent();
如果(库存数量<12)
{
if(other.gameObject.CompareTag(“玩家”))
{
存货。添加到存货(临时);
h、 SetActive(假);
}
}
}
生成以下错误的脚本:

严重性代码说明项目文件行抑制状态抑制状态 错误CS1503参数1:无法从“UnityEngine.GameObject”转换为“Item”程序集CSharp C:\Users\Owner\Downloads\Project 3(1)\Project 3\Assets\Scripts\Wearms.cs 63 Active

这是将项目添加到我的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();

        }
    }
}




//哈基姆·托马斯
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
//(Application.persistentDataPath+“/example.txt”);
公共类清单:单一行为
{
公共GUI_2D弹出窗口;
健康项目hpPo;
公共静态链接列表;
//私人物品[]快速物品;
字符串[]str;
整数数量;
公共规模;
void Start()
{
}
public void InitializeInventory()
{
invItems=newlinkedlist();
InvSize=受邀毫秒计数;
str=新字符串[12];
}
公共作废AddToInventory(项目newItem)
{
bool-found=false;
如果(毫秒计数<12)
{
foreach(MS中的项目x)
{
if(x.Name==newItem.Name)
{
x、 数量++;
发现=真;
打破
}
}
}
其他的
{
Log(“存储已满,无法再占用”);
}
如果(!found&&invItems.Count<12)
{
invItems.AddLast(newItem);
}
}
void OnGUI()
{
如果(弹出菜单)
{
BeginArea(新Rect(501001250400),“库存”);
Guillayout.空间(20);
int c=0;
如果(ms.Count>0)
{
foreach(MS中的项目x)
{
C++;
if(GUILayout.按钮(x.Name+“”+x.Quantity))
{
//str[invItems.Count]=x.Name;
x、 UseItem();
如果(x.数量>1)
{
x、 数量--;
}
其他的
{
移除(x);
}
}
}
如果(c>6){c=6;}
}
对于(int i=0;i<6-c;i++)
if(GUILayout.Button(“null”+“”+0)){}
GUILayout.EndArea();
}
}
}
我想做的是允许我的物品在取货时添加到我的库存中。我的列表是必需的。我不知道如何解决这个问题

我还有一个方法没有给出错误,但无法正确实现:

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
        {

        }
    }
void ontriggenter(碰撞器其他)
{
Item temp=other.GetComponent();
if(other.gameObject.CompareTag(“猎枪”))
{
add.AddToInventory(临时);
other.gameObject.SetActive(false);
wep.shotGun=真;
}
else if(other.gameObject.CompareTag(“小球”))
{
add.AddToInventory(临时);
other.gameObject.SetActive(false);
wep.pellet=true;
}
else if(other.gameObject.CompareTag(“Bargun”))
{
add.AddToInventory(临时);
other.gameObject.SetActive(false);
wep.barGun=真;
}
其他的
{
}
}

问题在于从
库存
类调用属性:

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);
            }
        }
    }
专用无效对撞机(对撞机其他)
{
Item temp=this.GetComponent();
//这个
如果(库存数量<12)
{
if(other.gameObject.CompareTag(“玩家”))
{
//这个
存货。添加到存货(临时);
h、 SetActive(假);
}
}
}
请注意,您使用了
Inventory.invItems
Inventory.AddToInventory()
,这将其视为调用静态字段。
(当您从类的名称而不是实例访问属性时,会发生这种情况。)

但是,这些属性都不是静态的,这会导致该错误。
这意味着您需要对
库存
对象的引用才能使用这些属性

您可能希望将这些属性设置为静态,或者改用单例

好书:


哪一行代码导致错误?(武器.cs的第63行是什么?)。有两个相同的代码。错误实际上是:>严重性代码描述项目文件行抑制状态抑制状态错误CS0120非静态字段、方法或属性“Inventory.AddToInventory(Item)”程序集CSharp C:\Users\Owner\Downloads\Project 3(1)需要对象引用\项目3\Assets\Scripts\HealthItems.cs 41Active@HakeemThomas你能分享你的
库存代码吗?我如何分享StackOverflow上的代码?