- Home /
Can a Vector3 be treated as an Array?
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#?
http://stackoverflow.com/questions/8380539/can-stdvector-be-treated-like-an-array
The C++ std::vector is a dynamic array. Completly different that Vector3
Answer by Bunny83 · Jul 15, 2014 at 11:35 AM
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;
Is it possible to complete the same task using a function:
function select3 (localscale : Vector3 , sel : int , newvar : float){
if (sel % 3 == 0) { localscale .x += newvar ; } // Yodamo
if (sel % 3 == 1) { localscale .y += newvar ; }
if (sel % 3 == 2) { localscale .z += newvar ; }
}
i think that localscale here is also a struct and not a class so it can't assign to the gameobject?
sel3(childs[ws].transform.localScale , selectionVar, 2);//
@drudiverse: No, that won't work. It has actually two faults. First of all your code is written in UnityScript, that's a completely different topic as UnityScript handles some of the built-in properties in a "special way". $$anonymous$$y example is written in C# as you didn't tell us what language you're using.
However your code doesn't make any sense. $$anonymous$$ethod parameters are always passed by value. In UnityScript you can't even declare a by-ref parameter as the languages doesn't support it. Even if you could pass the Vector3 by ref, it wouldn't work either since localScale is a property and returns a copy of the vector since Vector3 is a value type.
In UnityScript the compiler adds extra code if you access x, y or z of a the built-in property. For example this works in UnityScript:
transform.localScale.y += 0.5;
because the compiler actually generates this out of that one line:
var tmp = transform.localScale;
tmp.y += 0.5;
transform.localScale = tmp;
That's just a (quite evil since hidden) trick to "simplify" the usage but it actually just makes the whole compiler inconsistent. The following line doesn't work in UnityScript:
transform.localScale[1] += 0.5;
It seems strange since ".y" and "[1]" actually access the same value, but there's no magic code for this case.
A property is actually not a variable. It's a pair (one might be omitted) of two methods. One get-method and one set-method. Whenever you "read" a property you actually call the get method. If you "write" to the property you actually call the set method.
So this:
transform.localScale = Vector3.one;
is actually
transform.set_localScale(Vector3.one);
Now if you do this:
transform.localScale.y = 5;
you actually do
transform.get_localScale().y = 5;
You call the getter which returns a copy of the Vector3 and then you set the y component of that copy. However that doesn't call the setter so the localScale stays unchanged.
If you want to simplify this, you can create a method like that:
// UnityScript
function AddToLocalScaleComponent(t : Transform, sel : int, amount : float)
{
var tmp = t.localScale;
tmp[sel] += amount;
t.localScale = tmp;
}
@qato and UA: Thanks for finally removing that annoying autoscrolling / autofocusing when writing long comments. It drove me crazy. $$anonymous$$ost the time i was forces to continue writing in an external editor and copy them back in the end.
Answer by dsada · Jul 15, 2014 at 11:17 AM
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 :)
Answer by Kaz_Yamof · Jul 15, 2014 at 11:25 AM
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);
Answer by fafase · Jul 15, 2014 at 11:35 AM
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] += i;
}
transform.localScale = vec;
Answer by pauloapsantos96 · Jul 15, 2014 at 11:40 AM
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.
Your answer
Follow this Question
Related Questions
Accessing Vector3s of GameObjects in Arrays (JS) 1 Answer
Vector1, Vector2, Vector3? 1 Answer
Rotate Vector3 array around a point 1 Answer