kandi background
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Unity open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Unity

v6.1.8

v1.7.0 - Linux

ML-Agents Release 19

2022.409.0

PyBullet 3.22

dnSpy

v6.1.8

AirSim

v1.7.0 - Linux

ml-agents

ML-Agents Release 19

osu

2022.409.0

bullet3

PyBullet 3.22

Popular Libraries in Unity

Trending New libraries in Unity

Top Authors in Unity

1

287 Libraries

41824

2

200 Libraries

1928

3

194 Libraries

1473

4

140 Libraries

70479

5

70 Libraries

28622

6

50 Libraries

1006

7

47 Libraries

415

8

40 Libraries

3139

9

37 Libraries

4297

10

32 Libraries

1200

1

287 Libraries

41824

2

200 Libraries

1928

3

194 Libraries

1473

4

140 Libraries

70479

5

70 Libraries

28622

6

50 Libraries

1006

7

47 Libraries

415

8

40 Libraries

3139

9

37 Libraries

4297

10

32 Libraries

1200

Trending Kits in Unity

No Trending Kits are available at this moment for Unity

Trending Discussions on Unity

    How can I make a constant static immutable instance of a class?
    Unity3D Display a UnityEvent Corrrectly in a ReorderableList
    How do I avoid async void?
    Converting Numbers from Base 10 to Base 60
    React unity WebGL not working on React JS project
    Unity missing reference at Library/PackageCache/com.unity.collab-proxy@1.2.16
    Why is my character's horizontal velocity lost when jumping?
    How do I replace a switch statement over an enum with runtime-dynamic type-based generic dispatch in C#?
    Make the distance between neighbor notes the same (Theremin like app)
    Extracting the measurement unit (degrees, metres, etc.) from spatial data in R

QUESTION

How can I make a constant static immutable instance of a class?

Asked 2022-Mar-07 at 22:32

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:

1public static readonly Coord3 zero = new Coord3(0, 0, 0);
2

However I found that this variable can be changed. For example if I do

1public static readonly Coord3 zero = new Coord3(0, 0, 0);
2Coord3 coord = Coord3.zero;
3coord.x = 5;
4

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.

ANSWER

Answered 2022-Mar-07 at 21:41

readonly is not quite the same thing as immutable in the sense you mean. readonly means you cannot assign to a variable. So you couldn't do

copy icondownload icon

1public static readonly Coord3 zero = new Coord3(0, 0, 0);
2Coord3 coord = Coord3.zero;
3coord.x = 5;
4public static readonly Cord3 zero = new Cord3(0, 0, 0);
5
6zero = new Cord3(0, 0, 1);
7

In order to achieve the effect you want, you could need to create a class, struct or record with readonly properties or fields. There's no way to achieve that effect with a type defined in an internal library. If the type allows mutability on a field or property, that field or property is mutable.

Source https://stackoverflow.com/questions/71387522