【问题标题】:Can you help me with bug in array or enum in Unity3d project?你能帮我解决 Unity3d 项目中数组或枚举中的错误吗?
【发布时间】:2021-11-03 20:18:54
【问题描述】:

您好,我开始在 Unity3d 中开发游戏,但我的代码或检查器中有错误。

我的错误出现在我分配 itemToEquip 两次的数组 arthursButtonsDescription 中。第一轮一切顺利 arthursButtonsDescription[0].name 是“0”,但第二轮我分配 arthursButtonsDescription[4].name=4 和 arthursButtonsDescription[0].name 也是 4,这是我的错误。

我录制了有关此错误的视频:https://youtu.be/wZuFo5uhL5o

我将我的错误项目上传到:https://uloz.to/file/zfyxYuOiHTa4/dandd-zip#!ZGp4LmR2AzV0MTSvMGxkMwOvZwEuBGEuIRuhIyM6MJAgFGIuAj==/dandd-zip

using UnityEngine;
public class Item : MonoBehaviour
{
    public Sprite icon = null;
    public bool showInInventory = true;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Equipment : Item
{
    public EquipmentSlot equipSlot;

    new public string name = "New Item";
}

public enum EquipmentSlot { Head, Chest, Legs, Foot, Shield, Weapon, 
    Potion20Hp, Potion40Hp, Potion60Hp, Potion80Hp, Potion100Hp, 
    Potion25Percent, Potion50Percent, Potion75Percent, Potion100Percent }
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SaveLoadStarter : MonoBehaviour
{
    public void LoadClick()
    {
        Equipment itemToEquip = gameObject.AddComponent<Equipment>();

        itemToEquip.name = "0";
        itemToEquip.equipSlot = EquipmentSlot.Head;
        Debug.Log("iTE0: " + itemToEquip.name);
        EquipmentManager.instance.Equip(itemToEquip);

        itemToEquip.name = "4";
        itemToEquip.equipSlot = EquipmentSlot.Shield;
        Debug.Log("iTE4: " + itemToEquip.name);
        EquipmentManager.instance.Equip(itemToEquip);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EquipmentManager : MonoBehaviour
{
    public static EquipmentManager instance;
    public void Awake()
    {
        if (instance != null)
        {
            Debug.Log("EquipmentManager instance not equals null");
            return;
        }
        instance = this;
    }

    public ArthurInventory ai;

    public void Equip(Equipment newItem)
    {
        ai = FindObjectOfType(typeof(ArthurInventory)) as ArthurInventory;
        ai.ShowArthursButtons(newItem);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ArthurInventory : MonoBehaviour
{
    public GameObject[] arthursButtons/* = new GameObject[6]*/;
    public Equipment[] arthursButtonsDescription = new Equipment[6];
    public void ShowArthursButtons(Equipment newItem)
    {
        for (int i = 0; i < arthursButtons.Length; i++)
        {
            if (arthursButtons[i].name == "HellmButton" && newItem.equipSlot == EquipmentSlot.Head)
            {
                arthursButtons[i].GetComponentInChildren<Button>().GetComponentInChildren<Image>().enabled = true;
                arthursButtonsDescription[0] = newItem;
            }
            if (arthursButtons[i].name == "ShieldButton" && newItem.equipSlot == EquipmentSlot.Shield)
            {
                arthursButtons[i].GetComponentInChildren<Button>().GetComponentInChildren<Image>().enabled = true;
                arthursButtonsDescription[4] = newItem;
                Debug.Log("AI0: " + arthursButtonsDescription[0].name);
            }
        }
    }
}

【问题讨论】:

  • 您正在将相同的对象引用分配给数组中的多个位置 -> 您实际上正在更改同一个对象的值...您的 Item 类必须是 MonoBehaviour 在全部?

标签: c# arrays unity3d enums


【解决方案1】:

您的问题似乎出在 SaveLoadStarter 中。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SaveLoadStarter : MonoBehaviour
{
    public void LoadClick()
    {
        Equipment itemToEquip = gameObject.AddComponent<Equipment>();

        itemToEquip.name = "0";
        itemToEquip.equipSlot = EquipmentSlot.Head;
        Debug.Log("iTE0: " + itemToEquip.name);
        EquipmentManager.instance.Equip(itemToEquip);

        itemToEquip.name = "4";
        itemToEquip.equipSlot = EquipmentSlot.Shield;
        Debug.Log("iTE4: " + itemToEquip.name);
        EquipmentManager.instance.Equip(itemToEquip);
    }
}

您声明 itemToEquip,设置它,然后再次设置它。

您需要创建一个新的设备对象。否则,您只是在更改第一个对象中的值。

试试这样的:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SaveLoadStarter : MonoBehaviour
{
    public void LoadClick()
    {
        Equipment itemToEquip = gameObject.AddComponent<Equipment>();

        itemToEquip.name = "0";
        itemToEquip.equipSlot = EquipmentSlot.Head;
        Debug.Log("iTE0: " + itemToEquip.name);
        EquipmentManager.instance.Equip(itemToEquip);

        itemToEquip = gameObject.AddComponent<Equipment>();

        itemToEquip.name = "4";
        itemToEquip.equipSlot = EquipmentSlot.Shield;
        Debug.Log("iTE4: " + itemToEquip.name);
        EquipmentManager.instance.Equip(itemToEquip);
    }
}

【讨论】:

  • 这样你最终会在同一个游戏对象上拥有多个Equipment 组件,尽管这可能会在大多数情况下破坏东西......问题是@OP:你的Item 类甚至需要要成为MonoBehaviour 吗?
  • 非常感谢。这行得通。我希望有一天我会成为像你一样熟练的程序员。我认为我的 Item 类不需要是 MonoBehaviour。这会修复我的错误吗?
  • itemToEquip = new Equipment(); 会更好地解决这个错误吗?还是我应该销毁 itemToEquip?
  • @Mr.Koala 我不知道。根据文档 AddComponent “将 componentType 类型的组件类添加到游戏对象。”所以我想这取决于你是否需要游戏对象中的设备。我不会销毁 itemToEquip,因为您已将其添加到游戏对象中,除非您不再需要它。
  • @Trisped 如果它不是 MonoBehaviour 那么是的 new Equipment 将被允许并以相同的方式解决它......关键是:你对同一个对象有相同的引用在集合中的两个不同的地方......每次创建一个新实例都可以解决这个问题
猜你喜欢
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多