"itemSlotRectTransform" 是一个变量,但像类型一样使用?
"itemSlotRectTransform" is a variable but is used like a type?
我正在按照教程进行盘点(确切地说是这个 link to video)
代码中的一行如下:
RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<itemSlotRectTransform>();
我收到错误
"itemSlotRectTransform" is a variable
but is used like a type.
似乎没有其他人遇到此错误。
作为参考,这里是完整的代码块:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryUI : MonoBehaviour
{
private Inventory inventory;
private Transform itemSlotContainer;
private Transform itemSlotTemplate;
private void Awake()
{
itemSlotContainer = transform.Find("itemSlotContainer");
itemSlotTemplate = transform.Find("itemSlotTemplate");
}
public void SetInventory(Inventory inventory)
{
this.inventory = inventory;
RefreshInventoryItems();
}
private void RefreshInventoryItems()
{
int x = 0;
int y = 0;
float itemSlotCellSize = 30f;
foreach (Item item in inventory.GetItemList())
{
RectTransform itemSlotRectTransform =
Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<itemSlotRectTransform>();
itemSlotRectTransform.gameObject.SetActive(true);
itemSlotRectTransform.anchoredPosition =
new Vector2(x * itemSlotCellSize, y * itemSlotCellSize);
x++;
if(x > 4)
{
x = 0;
y++;
}
}
}
}
如果您能提供帮助,我将不胜感激!
通用参数必须是类型,而您指定的是类型的实例。
试试这个:
Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<RectTransform>()
注意尖括号中的参数。
我正在按照教程进行盘点(确切地说是这个 link to video) 代码中的一行如下:
RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<itemSlotRectTransform>();
我收到错误
"itemSlotRectTransform" is a variable
but is used like a type.
似乎没有其他人遇到此错误。
作为参考,这里是完整的代码块:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryUI : MonoBehaviour
{
private Inventory inventory;
private Transform itemSlotContainer;
private Transform itemSlotTemplate;
private void Awake()
{
itemSlotContainer = transform.Find("itemSlotContainer");
itemSlotTemplate = transform.Find("itemSlotTemplate");
}
public void SetInventory(Inventory inventory)
{
this.inventory = inventory;
RefreshInventoryItems();
}
private void RefreshInventoryItems()
{
int x = 0;
int y = 0;
float itemSlotCellSize = 30f;
foreach (Item item in inventory.GetItemList())
{
RectTransform itemSlotRectTransform =
Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<itemSlotRectTransform>();
itemSlotRectTransform.gameObject.SetActive(true);
itemSlotRectTransform.anchoredPosition =
new Vector2(x * itemSlotCellSize, y * itemSlotCellSize);
x++;
if(x > 4)
{
x = 0;
y++;
}
}
}
}
如果您能提供帮助,我将不胜感激!
通用参数必须是类型,而您指定的是类型的实例。
试试这个:
Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<RectTransform>()
注意尖括号中的参数。