我正在编写一个简单的脚本来创建一个库存系统,但我一直收到"预期标识符"错误。我正在尝试选择脚本附加到的当前项目,并将其添加到库存中。
这是错误:
资产\脚本\项目对象. cs(10,25):错误CS1001:需要标识符
我遵循的教程:https://www.youtube.com/watch?v=SGz3sbZkfkg
以下是库存系统:
InventorySystem.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventorySystem : MonoBehaviour
{
private Dictionary<InventoryItemData, InventoryItem> m_itemDictionary;
public List<InventoryItem> inventory {get; private set;}
private void Awake() {
inventory = new List<InventoryItem>();
m_itemDictionary = new Dictionary<InventoryItemData, InventoryItem>();
}
public void Add(InventoryItemData referenceData) {
if(m_itemDictionary.TryGetValue(referenceData, out InventoryItem value)) {
value.AddToStack();
} else {
InventoryItem newItem = new InventoryItem(referenceData);
inventory.Add(newItem);
m_itemDictionary.Add(referenceData, newItem);
}
}
public void Remove(InventoryItemData referenceData) {
if (m_itemDictionary.TryGetValue(referenceData, out InventoryItem value)) {
value.RemoveFromStack();
if(value.stackSize == 0) {
inventory.Remove(value);
m_itemDictionary.Remove(referenceData);
}
}
}
}
[Serializable]
public class InventoryItem {
public InventoryItemData data {get; private set;}
public int stackSize {get; private set;}
public InventoryItem(InventoryItemData source) {
data = source;
AddToStack();
}
public void AddToStack() {
stackSize++;
}
public void RemoveFromStack() {
stackSize--;
}
}
下面是处理项目拾取的脚本:
ItemObject.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemObject : MonoBehaviour {
public InventoryItemData referenceItem;
public void OnHandlePickupItem() {
InventorySystem.this.Add(referenceItem);
Destroy(gameObject);
}
}
如果有任何帮助,我将非常感激:)
1条答案
按热度按时间qmelpv7a1#
不能在
ItemObject
中使用InventorySystem.this.Add(referenceItem)
引用InventorySystem
。尝试以下操作:
InventorySystem
组件添加到场景中的游戏对象Inventory
的标签标记此游戏对象GameObject.findByTag
查找InventorySystem(在ItemObject
的Awake
方法中执行此操作