Question: How can I make a constant static immutable instance of a class?
I have a simple class that represents 3 dimensional coordinates called Coord3
that simply has x, y, and z int
values.
I want to declare a static constant variable Coord3.zero
where x, y, and z are set to 0.
I have attempted this with:
public static readonly Coord3 zero = new Coord3(0, 0, 0);
However I found that this variable can be changed. For example if I do
Coord3 coord = Coord3.zero; coord.x = 5;
this actually changes the x value of Coord3.zero
to be 5. Maybe I am misunderstanding readonly
? I know that in Unity there is Vector3.zero
which never changes. I am trying to achieve the same effect.