Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by drudiverse · Jul 15, 2014 at 11:13 AM · arrayvector3

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Opde · Jul 15, 2014 at 11:20 AM 1
Share

The C++ std::vector is a dynamic array. Completly different that Vector3

6 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image drudiverse · Jul 15, 2014 at 12:13 PM 0
Share

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);//
avatar image Bunny83 · Jul 15, 2014 at 08:22 PM 0
Share

@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;
 }

avatar image Bunny83 · Jul 15, 2014 at 08:29 PM 0
Share

@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.

avatar image
2

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing Vector3s of GameObjects in Arrays (JS) 1 Answer

Vector1, Vector2, Vector3? 1 Answer

Rotate Vector3 array around a point 1 Answer

2D Array and other Script method access 1 Answer

Spawn object from Random Vector3 in array 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges