Description
The Item
property of the Vector3
struct allows you to access or modify the individual components of the vector using an index. This property acts as an indexer for the Vector3
struct, enabling you to treat the vector as an array of three float
values.
Usage
Use the Item
property to access or set the x, y, or z components of a Vector3
instance by index. The index values are as follows:
0
for the x component
1
for the y component
2
for the z component
Attempting to access an index outside of this range will result in an IndexOutOfRangeException
.
Example
Vector3 vector = new Vector3(1.0f, 2.0f, 3.0f);
// Accessing components using the Item property
float x = vector[0]; // x = 1.0f
float y = vector[1]; // y = 2.0f
float z = vector[2]; // z = 3.0f
// Modifying components using the Item property
vector[0] = 4.0f; // vector is now (4.0f, 2.0f, 3.0f)
vector[1] = 5.0f; // vector is now (4.0f, 5.0f, 3.0f)
vector[2] = 6.0f; // vector is now (4.0f, 5.0f, 6.0f)