具有不同实例的c#Unity共享脚本
问题是我有一个土豆类,当我通过清单使用它时,它应该去掉土豆的用途,而只使用那个土豆。取而代之的是,它把游戏中的每一个土豆都去掉了,有没有办法让所有土豆都有自己的代码实例具有不同实例的c#Unity共享脚本,c#,unity3d,unity5,C#,Unity3d,Unity5,问题是我有一个土豆类,当我通过清单使用它时,它应该去掉土豆的用途,而只使用那个土豆。取而代之的是,它把游戏中的每一个土豆都去掉了,有没有办法让所有土豆都有自己的代码实例 public class Food : Item { public float healthHealedOnUse; public int uses; }//name ect is in the Item base class 我用于库存的代码: public void UseItem(){ i
public class Food : Item {
public float healthHealedOnUse;
public int uses;
}//name ect is in the Item base class
我用于库存的代码:
public void UseItem(){
if (item != null) {
if (item is Food) {
Debug.Log ("using "+item.name);
PHH.Heal (((Food)item).healthHealedOnUse);
((Food)item).uses--;
if(((Food)item).uses < 1){
ClearSlot();
}
} else {
item.Use ();
}
将项目添加到库存槽中的代码
public void AddItem(Item newItem){
item = newItem;
icon.sprite = item.icon;
icon.enabled = true;
removeButton.interactable = true;
}
如何在库存插槽中显示项目
Inventory inventory;
public GameObject Inventoryui;
public Transform itemsParent;
InventorySlot[] slots;
void Start () {
Inventoryui.SetActive (false);
inventory = Inventory.instance;
inventory.onItemChangedCallback += UpdateUI;
slots = itemsParent.GetComponentsInChildren<InventorySlot> ();
}
public void ToggleInventory(){
Inventoryui.SetActive (!Inventoryui.activeSelf);
}
void UpdateUI(){
Debug.Log ("Updating UI");
for (int i = 0; i < slots.Length; i++) {
if (i < inventory.items.Count) {
slots [i].AddItem (inventory.items [i]);
} else {
slots [i].ClearSlot ();
}
}
}
库存;
公共游戏对象清单;
公共设施;
库存槽[]槽;
无效开始(){
Inventoryui.SetActive(false);
inventory=inventory.instance;
inventory.onItemChangedCallback+=UpdateUI;
slots=itemsParent.GetComponentsInChildren();
}
public void ToggleInventory(){
Inventoryui.SetActive(!Inventoryui.activeSelf);
}
void UpdateUI(){
Debug.Log(“更新UI”);
对于(int i=0;i
…将其视为可编写脚本的项目
这就是你的“每个参考都是一样的”问题
您需要:
Potato
对象,但所有的Potato堆栈都是ItemStack
的一个实例,其中包含对理想化的Potato实例的引用)我们需要看到更多的代码。你是如何将物品添加到库存的?另外,你应该让你的
食物
类的Use()
方法治疗玩家。这就是子类化的要点:Item#Use
被标记为virtual
,然后Food#Use
被标记为override
,您只需调用UseItem()
Item.Use(),就完成了。好了,我在其中添加了关于Food的Use类的附加项,我认为这比找到玩家组件更好,然后从玩家身上取下玩家统计处理程序。我把这变成了你另一个问题的答案。关于这一点……我现在需要知道你在哪里打电话给add
:也就是说,我需要看看你是如何将多个土豆放入你的库存的。很可能你正在做类似于var POTATO=newfood();/*…*/Player.inventory.add(马铃薯)代码>导致所有库存项目都引用同一个对象。我在哪里克隆可编写脚本的项目?在预置中?@BrandonG在实例化预置和将预置添加到玩家的库存之间的任何时间。就我个人而言,我会把它作为您已有的add
方法的一部分。
public void AddItem(Item newItem){
item = newItem;
icon.sprite = item.icon;
icon.enabled = true;
removeButton.interactable = true;
}
Inventory inventory;
public GameObject Inventoryui;
public Transform itemsParent;
InventorySlot[] slots;
void Start () {
Inventoryui.SetActive (false);
inventory = Inventory.instance;
inventory.onItemChangedCallback += UpdateUI;
slots = itemsParent.GetComponentsInChildren<InventorySlot> ();
}
public void ToggleInventory(){
Inventoryui.SetActive (!Inventoryui.activeSelf);
}
void UpdateUI(){
Debug.Log ("Updating UI");
for (int i = 0; i < slots.Length; i++) {
if (i < inventory.items.Count) {
slots [i].AddItem (inventory.items [i]);
} else {
slots [i].ClearSlot ();
}
}
}