- Home /
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
}
Answer by aldonaletto · Dec 10, 2013 at 02:01 AM
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
}
Answer by The Kracken · Dec 10, 2013 at 01:50 AM
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
Your answer
Follow this Question
Related Questions
Change Integer to All Gameobjects With Script 1 Answer
How can i get a Transform component from an other object's script? 1 Answer
How do I change Sprites (and manipulate the Sprite Object) in C#? 0 Answers
problem of switching character in the game 0 Answers
NullReferenceException: Object reference not set to an instance of an object ..... 1 Answer