C# 对象实例化后,自定义游戏对象数组的索引是否返回null?
在运行时,我首先向数组中添加一些项,然后将它们的值写入日志(工作正常),然后实例化它们(仍然工作正常),然后我想更改数组项的值,但由于某种原因,现在数组索引返回null?谁知道出了什么问题C# 对象实例化后,自定义游戏对象数组的索引是否返回null?,c#,arrays,unity3d,singleton,gameobject,C#,Arrays,Unity3d,Singleton,Gameobject,在运行时,我首先向数组中添加一些项,然后将它们的值写入日志(工作正常),然后实例化它们(仍然工作正常),然后我想更改数组项的值,但由于某种原因,现在数组索引返回null?谁知道出了什么问题 public class ListEquippedItems : MonoBehaviour { private static ListEquippedItems instance = null; public static ListEquippedItems Instance { get
public class ListEquippedItems : MonoBehaviour
{
private static ListEquippedItems instance = null;
public static ListEquippedItems Instance
{
get
{
if (instance == null) instance = new ListEquippedItems();
return instance;
}
}
[SerializeField]
private GameObject[] currentlyEquippedItems = new GameObject[8];
void Awake()
{
// Putting fake items into the currently equipped to test that items can actually be transfered to it
for (int i = 0; i < currentlyEquippedItems.Length; i++)
{
//////////////////// In 3 below lines the array works just fine and returns proper values.
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemType((EffectTypes)i);
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemName("I am item: " + i);
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemIcon(RewardAssetContainer.Instance.commonExample);
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemIcon().ToString());
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemType().ToString());
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemName());
}
}
public void SetEquippedItem(EffectTypes _itemType, Sprite _itemIcon, string _itemName)
{
for (int i = 0; i < currentlyEquippedItems.Length; i++)
{
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemType().ToString());
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemName());
Debug.Log(currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemIcon().ToString());
//////////////////// In 3 below lines the array returns null references, but why?
if (currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().GetItemType() == _itemType)
{
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemName(_itemName);
currentlyEquippedItems[i].GetComponent<ListEquippedEntry>().SetItemIcon(_itemIcon);
break;
}
}
}
公共类ListQuipedItems:单行为
{
私有静态ListQuippedItems实例=null;
公共静态ListQueppedItems实例
{
得到
{
如果(instance==null)instance=new listQuipedItems();
返回实例;
}
}
[序列化字段]
私有游戏对象[]CurrentlyEquipmeItems=新游戏对象[8];
无效唤醒()
{
//将假冒物品放入当前装备的测试系统中,以测试物品是否可以转移到该系统中
对于(int i=0;i
我想问一个澄清问题,但我没有代表对此发表评论,所以我想尝试一下
您的ListQuipedItems编码类似于singleton,但从技术上讲它不是singleton。我猜调用SetEquipItem()的对象与调用Awake()的对象不同
这可能发生的方式:
if(此!=实例)
{
Log(“针对非单例实例调用”);
}
如何解决这个问题:
仅以一种方式创建ListQuipedItems。如果要将其保留在继承人权限中,请将实例存储在Awake()方法中,而不是在引用实例字段时
如果你想要一个合适的单身汉,请在Unity wiki上查看。wiki条目上似乎有一个stackoverflow讨论。我想问一个澄清问题,但我没有代表评论,所以我只想尝试一下 您的ListQuipedItems编码类似于singleton,但从技术上讲它不是singleton。我猜调用SetEquipItem()的对象与调用Awake()的对象不同 这可能发生的方式:
if(此!=实例)
{
Log(“针对非单例实例调用”);
}
如何解决这个问题:
仅以一种方式创建ListQuipedItems。如果要将其保留在继承人权限中,请将实例存储在Awake()方法中,而不是在引用实例字段时
如果您想要一个合适的单例,请从Unity wiki上查看。wiki条目上似乎有一个stackoverflow讨论。我不知道您的_itemType的类型。但是您可以尝试同时添加toString()。例如.GetItemType().toString()==\u itemType.toString())我尝试了这个allready。我非常确定没有其他脚本以任何方式访问这个数组,而且游戏对象也不会被禁用或类似的东西。我不知道为什么它会在我身上返回空值,但项目和它们的精灵在场景中仍然可见--我看不到你提供的代码有任何问题,不是吗在这里,您可以以任何方式发布
SetItemName
、SetItemType
和SetItemIcon
的代码,因为它们似乎是ListEquippedEntry
类的成员,我相信这可能会产生一些影响。乍一看,您的代码似乎应该可以正常工作。结果如何
if( this != instance )
{
Debug.Log( "<methodName> invoked against non-singleton instance." );
}