I have this GameObject that must be initialized by a call of a function. The object has a color and a background color that usually are black and gray, but not always… This object is part of a collection - all with the same color according to the parent, that’s why is made like this. So it’s a simple case right?
public class digit : MonoBehaviour
{
public Color color { get; private set; }
public Color offColor { get; private set; }
public bool point { get; private set; }
public string twoPointsMode { get; private set; } // off, on, blink
private List<RawImage> Bit = new List<RawImage>();
private Color defaultColor = Color.black;
private Color defaultOffColor = Color.gray;
public void Ini(Color? color = null, Color? offColor = null, bool point = false, string twoPointsMode = "off")
{
this.color = (color != null) ? color : defaultColor;
this.offColor = (offColor != null) ? offColor : defaultOffColor;
...
}
But the compiler won’t allow me to define a default value for Color types, must be a core compiler const, and Color can’t be const. So I tried allowing null value by “?” and also with the null declared (though is the same result) and I will set the colors if null. But those lines generates error:
Compiler doesn’t make the simple semantic analysis: this.color will never get the null value, it’s a logical impossibility. An usual mistake, by the way, AI din’t come to us developers yet…
I will have to always define colors, even for simple tests, or there is a way to deal with this?
Essentially Color? is not the same type as Color. When you write Color?, compiler substitutes it with Nullable, wich is special class for wrapping value types for them to be able to represent null values. Of course you can’t convert Nullable to color because implicit conversion operator is not defined for that and will never be because you need to check nullable for null before using them. Nullable has .Value property of type Color. Rewrite your code:
this.color = color == null ? defaultColor : color.Value;
Thanks! I confess I’d read it twice before understood. I was leaded to an error because of the tip given in Visual Studio (maybe a translation problem). The type “?” will change the class, not just allow null value. I am still with JavaScript and php languages in my mind and those languages got a different concept. I am not an expert in logic part of classes conception (or philosophical part) but thought C# might be annoying but is generally more “elegant” - there is a good reason behind how things works or not. However, the compiler is still strange sometimes…
if (condition) int myNumber = 0
else int myNumber = 1
This will generates an error later as “myNumber” is not in the same context
int myNumber
if (condition) myNumber = 0
else myNumber = 1
This is the correct way, even if the previous one make the same thing that is to create and initialize “myNumber”. This kind of situation happens to me, and it’s difficult to find the bug…
The variable is defined for the scope of whole function, you can’t define two vars and give them same name. I suggest you read Jeffrey Richter’s book on c# , hes explanations are very clear more important, they are short and contains almost no water.