- Home /
Is it possible to loop through the variables of a custom object?
I have an Inventory script with a custom class InventoryItem, which has a bunch of stat modifier variables. At one point in OnGUI I need to loop through an InventoryItem to look at each of its stat modifier variables, and if whatever one it is currently on is greater than zero, display it in the statistics window.
In vanilla javascript I would do something like this:
for(var key in Item){
if(Item[key] > 0){
doSomething();
}
}
But in Unity I get an error: "Type 'InventoryItem' does not support slicing". Is there any way I could accomplish what I'm aiming for?
Answer by StephanK · Jul 01, 2010 at 09:08 PM
if you do for(var key in Item) it tries to enumerate the type Item, which isn't possible. You should instead create a Dictionary and iterate over that. Otherwise you could also use Reflection to achieve this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063644im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Best way to store a new variable in each iteration of a for loop? 1 Answer
Accessing variables of objects in scene and not in assets 1 Answer
Values in Array Automatically Revert 1 Answer
BCE0048: Type 'Object' does not support slicing 1 Answer
How to pass variables from one object in one scene to another object in another scene? 3 Answers