GetComponent is not member of Object?

I’m trying to make an inventory system and I want to show the description of a GameObject when it’s clicked on. The code goes like:

if(GUILayout.Button(inventoryNames[x].ToString(),GUILayout.Width(100), GUILayout.Height(50))){
    GUI.Box(Rect(50, 100, 50, 100), inventory[x].GetComponent(itemScript).description); //PROBLEM LINE
}

2 Answers

2

What type of array/list is inventory?

try this

inventory[x].gameObject.GetComponent<itemScript>().description

Inventory was an ArrayList, changing it to List. got rid of all the compiling errors. However, if I do inventory.Add(item.gameObject); it seems like nothing is added. Thanks for the help! edit: problem was unrelated

–

Yes, GetComponent isn’t part of Object - Object is a variant type, usually assumed by Unity when the compiler can’t infer the correct type, or in elements of the Array class. Is inventory[] an Array()? If so, assign the element to a variable of the right type before using GetComponent:

if(GUILayout.Button(inventoryNames[x].ToString(),GUILayout.Width(100), GUILayout.Height(50))){
    var item: GameObject = inventory[x]; // assuming that inventory contains GameObjects
    GUI.Box(Rect(50, 100, 50, 100), item.GetComponent(itemScript).description); //PROBLEM LINE
}

This solved it, thanks!

–