this line compiles and runs ok, but the size of the gameObject doesnt actually change when it should:
childs[ws].transform.localScale[ 1 ] += 2.0;
i want to select between x, y , z of the Vector3 as if it was an array. apparently it works in C#?
this line compiles and runs ok, but the size of the gameObject doesnt actually change when it should:
childs[ws].transform.localScale[ 1 ] += 2.0;
i want to select between x, y , z of the Vector3 as if it was an array. apparently it works in C#?
Yes you can. As the documentation says it has operator.
this[int] – Access the x, y, z components using [0], [1], [2] respectively.
And FYI the c++ std vector has nothing in common with this vector3
Yes, you can. But just to make clear: why you want this? You want add 2.0 to x,y,z in a for loop? If yes, you can just do
yourVector += new Vector3(2, 2, 2);
Yes, vector3 has an indexer, however your line of code has a different problem. localScale, position, localPosition, localRotation, … they are all properties and not simple fields. This usually doesn’t matter, however since Vector3 is a value type (it’S a struct, not a class) the property returns a copy when you execute your line of code. Then you change the y value of that copy which doesn’t do anything to the actualy localScale. To set a new localScale the setter of localScale has to be called by assigning a Vector3 to it.
You have to use either a temp variable, or work with Vector3 values only:
Vector3 tmp = childs[ws].transform.localScale;
tmp[ 1 ] += 2.0;
childs[ws].transform.localScale = tmp;
or
childs[ws].transform.localScale += new Vector3(0 ,2.0f, 0);
or
childs[ws].transform.localScale += Vector3.up * 2.0f;
You can but you cannot modify it directly in C# (dunno about Us).
You have to first store it and then modify the new vector and assign back:
Vector3 vec = transform.localScale;
for(int i = 0 ; i < 3 ; i++){
vec *+= i;*
}
transform.localScale = vec;
Well i don’t know if you can treat it like an array but to change the scale on one coordinate only in c# you can do it like this:
transform.localScale = new Vector3 (
transform.localScale.x,
transform.localScale.y + 2.0f,
transform.localScale.z
);
And the value you set to the coordinate of the vector must be of type Float or Int.
Hope this helps.
transform.localScale[1] is essentially the same as transform.localScale.y. You can treat a vector like an array, but you can’t do it like that.
This is why:
transform.localScale is a property that gives you a copy of the transform’s local scale. So when if you say transform.localScale.y (or localScale[1]) += 2, you are getting a copy of the local scale and changing the y component of that copy (but not the y component of the original), then the copy disappears because you aren’t storing it in a variable.
This is how to fix it:
What you actually need to do is store the copy, change the copy, and then assign the copy back to the localScale property.
Vector3 localScale = transform.localScale;
localScale[1] += 2;// or localScale.y += 2;
transform.localScale = localScale;