Home > OS >  "itemSlotRectTransform" is a variable but is used like a type?
"itemSlotRectTransform" is a variable but is used like a type?

Time:02-11

I'm following a tutorial to make an inventory (this one to be exact link to video) a line in the code goes as follows:

RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<itemSlotRectTransform>();

i get the error

"itemSlotRectTransform" is a variable 
but is used like a type.

nobody else seems to get this error.

For reference, here is the full bloc of code:

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  ;
            }
        }
    }
}

if you could help I'd much appreciate!

CodePudding user response:

Generic arguments must be types whereas you specified an instance of a type.

Try this instead:

Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<RectTransform>()

Notice the argument in angle brackets.

  • Related