- Home /
C# public decimal variable not showing up in inspector
Item Class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Items
{
public string strName;
public float fPrice;
public decimal dPrice;
public GameObject goItem;
public string getName
{
get { return strName; }
set { strName = value; }
}
}
When a list is created from this class, fPrice
shows up in the inspector but dPrice
doesn't. Any ideas why?
Do you really need a decimal for dPrice? That's the only reason I can guess it would not show up.
It's not necessary, but I am trying it out because my game itself deals with money rather heavily (including cents, not just dollars), that's why I thought perhaps using decimal might be better since I need to show the 0s at the back without concatenating them at the back of the string.
Answer by Ashkan_gc · Sep 30, 2014 at 07:11 AM
Only limited few types are serialized by unity and copied in Instantiate and shown in inspector. int and float are the only number types if I remember correctly. So short, uint, long and many others are not shown. First of all these types are not usually used in games, secondly they wanted a simple and fast serializer with the things required and thirdly because they realized the need to change this in some situations, they added support for custom serialization code using an interface in 4.3.
In this way only those who use the feature will pay for it and their serialization code will not become hard to maintain as well.
Answer by mattyman174 · Sep 30, 2014 at 07:03 AM
I would imagine its because Decimal is used for very high precision and an accidental change to a Decimal number especially in a public variable exposed in the inspector could have unforeseen consequences.
Take these 2 numbers for example.
1.100000
1.1
On the surface you could say they are identical, simply enough if you store either one as a Float it will be 1.1. However stored as a Decimal the 2 numbers are very different. Decimal is for high precision and will store the trailing 0's and i would imagine to avoid confusion they do not make Decimals visible in the inspector.
I'm curious as to why your using a Decimal in the first place. System.Decimal is stored as 128bits and unless you intend to use the precision i would recommend you stick with Float.
$$anonymous$$y only reason for wanting to use that is because of the monetary system in my game, since I have cents in them and need to display 0s at the back; I have not changed my codes, though, they are still functioning from my float variables until I find a better alternative.
You should look into string formatters to show trailing zeros http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.90).aspx