Is a struct technically a new data type

So from my understanding vector2, vector3, GameObjects, ect. are just structs, but they kinda act as a variable. So for example if I make a struct called newStruct and it contains 5 floats,
struct newStruct
{
float x;
float y;
float z;
float a;
float b;
}
is this a new data type that I can use?
Kind of a stupid question although I just want to clarify.

Thanks,
John

Yep, in similar manner as the class.

Simply try it.

1 Like

A struct is a value type, whereas a class is a reference type.

What this means is the struct data is actually stored with the object… so if you pass a struct into a function, that function now has its own version of that data (unless you use the ‘ref’ keyword). Any changes to the struct inside the function will not affect the original struct which was passed in. If it was a class, any changes made inside the function are actually altering the original class, as the function version is just a reference to the original, does that make sense?

A struct is generally better to use if possible as it can be allocated on the stack (fast). Structs have a few downsides which you will find as you use them.

If you just need a friendly name to group a bunch of variables together, use a struct over a class.

Hope this helps.

1 Like

Thank you this does help!

1 Like