Query related to struct of color32

what is the meaning of struct when we try to create new color32 it shows struct instead of class and also what is the difference between them?


Welcome to our community! :slight_smile:

You could regard a struct as a mini class. We usually create structs to bundle data of “simple” types. A popular example are coordinates with 2 or 3 variables of type int or float. If you take a look at Vector2 and Vector3, you’ll notice that they are structs.

The same idea applies to Color and Color32.

Did this clear it up for you?

See also:

Also, structs are value types, while classes are reference types. What this means is that when you assign one to a variable, a new copy is made. When you do that with classes you assign the reference, so the variable is not a copy, but the same thing. Here’s an example;

When we have a class (assume ‘Health’ is a MonoBehaviour - which is a class)

Health myHealth = GetComponent<Health>();
Health myHealth2 = myHealth;

Both myHealth and myHealth2 reference the same Health component. If I change a value on myHealth, it is also changed on myHealth2.

But with a struct (like Color32)

Color32 myColor = new Color32(40f, 40f, 40f, 255f); // darkish grey
Color32 myColor2 = myColor;

myColor2 is a copy of myColor. Only the value was assigned. If you change myColor in any way it does not affect myColor2.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms