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 Limbo · Feb 18, 2015 at 09:59 PM · script.assemblybuilt-in

Is there a way to override the Vector3 class or edit it?

I want to optimize the way it works , because i do not like creating a new Instance of the object (Vector3) each time I want to modify a single property.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by tanoshimi · Feb 18, 2015 at 10:43 PM

Sure. Define an extension method.

Comment
Add comment · Show 4 · 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 Limbo · Feb 18, 2015 at 11:27 PM 0
Share

Thought about them, but i really need to create some properties too. Vector3 class has a Set method but doesn't work for some reason.

avatar image Bonfire-Boy · Feb 19, 2015 at 12:32 AM 0
Share

The above is the answer to the question you asked. If you want to make altering some property easier (that can't already be easily altered), then you should use an extension method. It would be wrong to characterise this as "optimising" it, though. Your extension method would be using the methods that are already available, just wrapping them in something to make your life easier.

If (totally different question) you want to add fields as well as methods, your best bet (because Vector3 is sealed) might be to create a class that "wraps" a Vector3 ie has one as a member.

Vector3.Set does work, by the way. Are you by any chance trying to do something like transform.position.Set() in a GameObject? That won't change the object's position because the Vector3 is is being passed by value (so in that, Set() only changes the local copy). To change the transform's position you have to do transform.position = ... (or use one of Transform's member functions that modifies it of course).

I suspect the above to be the root of your confusion. It's very easy to change individual values of a Vector3 as others have pointed out. It's just that you may not be altering the Vector3 that you think you are.

avatar image Limbo · Feb 19, 2015 at 06:23 PM 0
Share

I do understand and I know what optimising means, stop being condescending people! And what i wanted is to modify the class Vector3 but it is sealed i suppose i have to wait until unity actually let us do this... And thank you for answering.

avatar image Bonfire-Boy · Feb 19, 2015 at 06:34 PM 2
Share

I am truly sorry if you found it condescending, but the point of commenting on language is that sometimes it's not clear what people mean, and pointing out misuse of words can help point towards where they can make things clearer. Getting you to be clearer with your wording makes it easier for other people to assist.

The fact that it is so easy to change the basic individual properties of a Vector3 (as Zarentyx's answer points out) makes it difficult to be certain what it is you're actually looking for. You may have actually been looking for an optimisation, which is why I said that one of my interpretations of your question wouldn't count as optimisation.

It's still not clear what you're looking for. So can you give an example of a property of a Vector3 that you want to be able to change, that isn't already very easy to change?

avatar image
1

Answer by Pangamini · Aug 06, 2019 at 12:59 PM

You don't have to worry about creating an instance of a value type (a struct), as long as you are not boxing it . Also, you can easily overwrite the single element of the vector, it's x,y,z are public variables

Comment
Add comment · Show 1 · 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 Bunny83 · Aug 06, 2019 at 03:44 PM 0
Share

I'm pretty sure after 4 years he doesn't really struggle with this anymore ^^. The issue he might had referred to is probably the fact that you can not change individual components directly when you're working on properties like Transform.position or Transform.eulerAngles.


So this does not work:

 transform.position.x = 5;

and you have to do

 Vector3 tmp = transform.position;
 tmp.x = 5;
 transform.position = tmp;

Though I'm pretty sure he now understands value types, properties and that "new Vector3" does not create a "new object". Anyways +1 even though it doesn't address the question title but the indirect question in the description.

avatar image
0

Answer by Jessespike · Feb 18, 2015 at 10:50 PM

You can write extension methods. http://shattereddeveloper.blogspot.ca/2012/10/making-use-of-c-extension-methods-in.html

I wouldn't call these optimizations, you're actually adding complexity. The difference is negligible though, so don't worry about it. There's actually a good reason why Vector3 behaves the way it does ( http://forum.unity3d.com/threads/is-new-vector3-x-y-z-costly.105480/ )

Comment
Add comment · Show 1 · 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 Limbo · Feb 18, 2015 at 11:27 PM 0
Share

No extension methods, I know about them.

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

25 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

Related Questions

Compile Error (CS0246) appears for every script in the project. 0 Answers

Loading Script from Asset Bundle in iOS and WebGL. 2 Answers

New Script Assemblies not generating 0 Answers

In part assembly project, How can I check the part status whether it is fixed to correct place or not ? 0 Answers

NetcodeForGameObjects compilation error 1 Answer


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