我有一个 list 脚本,我需要找到一个游戏对象并获取它的组件(碰撞器),然后启用它。这是我的代码,你能告诉我哪里出错了吗。
public GameObject[] Inventory = new GameObject[3];
public Image[] InventorySlots = new Image[3];
public GameObject Ending;
private void Start()
{
Ending = GameObject.Find("End Determination Object").GetComponent<Collider2D>; //this doesn't work
}
public void AddStoryItem(GameObject item)
{
bool itemAdded = false;
//to put items in inventory
for (int i = 0; i < Inventory.Length; i++)
{
//check for empty slot
if (Inventory[i] == null)
{
//place item
Inventory[i] = item;
InventorySlots[i].overrideSprite = item.GetComponent<SpriteRenderer>().sprite;
Debug.Log(item.name + " hey you got an item");
itemAdded = true;
item.SendMessage("Store");
break;
}
}
//inventory full
if (!itemAdded)
{
Debug.Log("it's full");
//enable collider here
}
}
看这个:
public GameObject Ending;
private void Start()
{
Ending = GameObject.Find("End Determination Object").GetComponent<Collider2D>; //this doesn't work
}
它不会工作因为Ending
是 GameObject
的一种但你正在分配 Collider2D
当你这样做的时候Ending = GameObject.Find("End Determination Object").GetComponent<Collider2D>;
这会起作用(因为 GameObject.Find
返回 GameObject
的类型):
Ending = GameObject.Find("End Determination Object");
但是既然你想找一个Collider2D
不是GameObject
改变
public GameObject Ending;
到
public Collider2D Ending;
现在您可以:Ending = GameObject.Find("End Determination Object").GetComponent<Collider2D>();
.
通知()
我在最后添加因为 GetComponent
是一个函数。
我是一名优秀的程序员,十分优秀!