Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Lemo76 · Oct 17, 2015 at 02:08 PM · javascriptbugglitchlogicbasic

Basic Logic makes absolutely no sense [JS]

Please help, I've got this simple test code, and it's producing the incorrect result. It's been bothering me for the past hour, and I still don't get what's going on. The code loops in an update function:

 print ("Before: " + numA.transform.position.x);
 numA.transform.position.x = 1;
 print ("After: " + numA.transform.position.x);

The output result it produces:

alt text

numA.transform.position.x is not referenced anywhere else in any other scripts. Any clues on as to why this is happening?

1.png (7.9 kB)
1.png (7.1 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DiegoSLTS · Oct 17, 2015 at 02:23 PM

Get the position into a variable, update the x value, set the position again. That's how it's done in C#, I thought that UnityScript did this automatically when you tried to update one component of the position, but I may be wrong.

This happens because Transform.position is a property and to set the property you have to set a new value on it. When you write transform.position it returns a NEW vector3, so when you change the x component of that vector the actual position (a private Vector3) is not being modified.

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 maccabbe · Oct 17, 2015 at 03:41 PM

The reason transform.position is not updating is because

  1. Vector3 are structs (value types) instead of a class (reference type)

  2. transform.position is being accessed using a property instead of a field

To elaborate 1: How do I know that Vector3 is a struct (value type) and not a class (reference type)? The information is available in the documentation.

http://docs.unity3d.com/ScriptReference/Vector3.html

Deciding what is a struct or a ref is a design choice and in this case using struct for Vector3 is the correct choice because it is small and always requires the same amount of memory.

https://msdn.microsoft.com/en-us/library/t63sy5hs.aspx

However using a value type means that setting a value equal to another value type copies all the data. Vector3s are are value types so

 var v1=Vector3.zero;
 var v2=v1;
 var v2.x=1;

would result in v1= (0, 0, 0) and v2 = (1, 0, 0).

in contrast, Mesh is a class (reference type) so doing the following

 var mesh1=new Mesh();
 var mesh2=mesh1;
 mesh2.subMeshCount=2;

would result in mesh2.subMeshCount=2 and mesh1.subMeshCount=2 even though mesh1.subMeshCount was never assigned to

To elaborate 2: transform.position is a property instead of a field. You can assume that everything in UnityEngine is a property and not a field. So when you do

  transform.position.x = 1;

what you are really doing is

 var v2=transform.position;
 v2.x=1;

https://msdn.microsoft.com/en-us/library/9d65as2e(v=vs.90).aspx

https://msdn.microsoft.com/en-us/library/ms173118.aspx

https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx

Now combine the two:

 transform.position.x = 1;

is the same as

 var v2=transform.position;
 v2.x=1;

and since Vector3 is a value type this results in transform.position = (0, 0, 0) even though v2 = (1, 0, 0).

To change transform.position you will have to do something like

 var v2=transform.position;
 v2.x=1;
 transform.position=v2.x;

The reason I bring up both that Vector3 are value types and transform.position is a property is because if either Vector3 was a reference type or if transform.position was a field your code would have worked as written. This is a good argument for why a beginner should learn C# instead of javascript. In C# transform.position.x = 1 doesn't even compile which is good because, as you pointed out, it can't be used for what you would except. You could use it for something in javascript but again, you'd run into the issue of someone (including yourself) reading your code excepting a different behaviour.

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

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

29 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 avatar image

Related Questions

HDRP Wierd lighting 0 Answers

My UNITY is not loading 0 Answers

Script function being applied to everything it's attached to logic error 2 Answers

Plane and Terrain Bug? 0 Answers

Character glitches at end of timeline 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