- Home /
How to make a custom type's contents modifiable in an array by inspector?
Hello everyone,
I apologize if my question is too vague, but I'm kind of a novice at this sort of thing.
My idea is to have collectibles objects in my game, with a only a few strings of data and a sprite, and to call this info into a button/list when its toggled.
But I'm not sure how to do this? I've made a custom data type with the fields for the information in the Collectible Class, and a parent class which has an array for the collectible class, but when I expand the array in the inspector to hold the nested Collectible infos, it just shows up as Empty
I've attached a snippet script of what I'm getting at
public class BadgeInfoRepo : MonoBehaviour
{
public BadgeInfo[] badgeInfoRepo; /*<- would like to have, say 10 BadgeInfos and fill their contents in the inspector*/
}
public class BadgeInfo : MonoBehaviour //an idea to make a container style type
{
public int badgeNum;//the badge number
public bool badgeCollected;//collected yet
public Sprite badgeIcon;//the icon
public string badgeName;//the name
public string badgeDescription;//a quick description of it
public string badgeCost;//the cost of the badge
}
How could I get this thing to work? Thank you!
Answer by IINovaII · Nov 02, 2019 at 01:33 PM
Instead of public BadgeInfo[] badgeInfoRepo;
, use public List<BadgeInfo> badgeInfoRepo;
.
You'll be able to see the collectible info in the inspector.
As for the Collectable info (if that only contain variables to store data), don't inherit from MonoBehaviour. Something like this would suffice.
[Serializable]
public class BadgeInfo
{
public int badgeNum;//the badge number
public bool badgeCollected;//collected yet
public Sprite badgeIcon;//the icon
public string badgeName;//the name
public string badgeDescription;//a quick description of it
public string badgeCost;//the cost of the badge
}
This worked perfectly! Thank you so much! I did have to add " using System " namespace at the top for others who might find this later.
Back to work/play now :)
Your answer
Follow this Question
Related Questions
Public Array with a class not showing in Inspcetor 1 Answer
See MonoScript's attributes in the inspector (when it is in an array) 0 Answers
How to show an array of custom objects in Inspector? 2 Answers
Inspector Doesn't Update Array Elements When Updated In Array Constructor 1 Answer
Calling an object from Array/List 1 Answer