- Home /
Taking variables out a struct
So i have a simple question. I have a structs with 2 variables inside of it. but how can I take them out of there to use them in a normal script. yes the variables have a value. it's 1 string value and 1 int value.
It's just mystruct.var1
and mystruct.var2
. If that doesn't make sense, you might want to add some code your question.
the vars doesn't show up public struct Cost { public string item; public int amount;
public Cost(string _item, int _amount)
{
item = _item;
amount = _amount;
}
}
Answer by AurimasBlazulionis · Jan 24, 2017 at 06:18 PM
First of all, Make variables inside the struct and the actual struct definition public, then you can just call theStructInstance.theVariable
to read the variables. If you need to edit them, you will need to either recreate the struct or use class instead of struct.
Also, if you want to have all the struct variables to show up in the inspector, add [System.Serializable]
above struct definition.
Your answer
![](https://koobas.hobune.stream/wayback/20220612105514im_/https://answers.unity.com/themes/thub/images/avi.jpg)