Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Sep 20, 2021 at 04:01 PM by aviiix for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by aviiix · Sep 16, 2021 at 10:12 PM · gameobjecttransformpositionloopdictionary

Why is my variable updating when it shouldn't?

I have a globally declared private dictionary variable named joint that takes a string and a class A

This gets populated in the Start() function. I use joint later on in a function named SetPosition() which gets called via Update(). And I use joint in this way:

var position = joint[i].transform.localPosition 2;

I was expecting
joint to maintain its original localPosition values over time considering how it was populated in the Start() function, but for some reason, when I output it, the localPosition in joint changes over time even though I only use it in the arithmetic. I never assign anything to joint.

There is another variable (
modifiedJoint) of the same type and contains the same data as joint, but this is the one I expect to have the changes over time and is populated in SetPosition(). It gets assigned the localPosition changes based on the arithmetic above.

I was intending to use
joint as a container for the original transforms, so that when trying to do arithmetic, I still have the original positions of the gameObject. I need this info because I am trying to move a gameObject forward/backward, and I need to return the gameObject to its original position once it moves "backwards". But evidently the joint position values change, and I don't know why they do.

Is doing arithmetic with transform.localPosition enough to change a gameObject's transform automatically without assignment?

Comment
Add comment · Show 7
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 Bonfire-Boy · Sep 17, 2021 at 02:39 PM 1
Share

A Dictionary of type Dictionary<string,A> contains references to objects as its Values. If they move, then their positions as accessed through the Dictionary will change (even though the Dictionary itself hasn't).



If you want to just store the initial positions so you've got them to use later, why not just store the initial positions? i.e use a Dictionary<string, Vector3>. The positions stored in the Dictionary will then be fixed until you change them explicitly via the Dictionary.

avatar image Bonfire-Boy · Sep 17, 2021 at 02:41 PM 0
Share

Also, can't think what you mean by "globally declared private variable" - am assu$$anonymous$$g the "globally declared" was a mistake.

avatar image logicandchaos Bonfire-Boy · Sep 17, 2021 at 08:52 PM 0
Share

"globally declared private variable" means it is global scope in the script and private for protection level.

avatar image Bonfire-Boy logicandchaos · Sep 20, 2021 at 10:40 AM 0
Share

I'm still confused, sorry. To me, the very idea of something being "global" precludes the possibility of it having a "protection level". What am I missing? Perhaps a bigger code snippet showing the structure of the file/class and where/how the joint variable is declared would help.


I'm just wondering if there's something in this that's relevant to the issue you were having, it's not clear to me yet whether or not my other comment about the Dictionary's Values being references was actually the cause of your problem.
Show more comments
avatar image aviiix · Sep 20, 2021 at 03:55 PM 0
Share

@Bonfire-Boy yes, that is correct. It was the dictionary causing the issue

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by VisionElf · Sep 20, 2021 at 01:56 PM

You are registering a reference to the Transform. The transform is changing position, you'll have a different value for transform.localPosition.

You might want to store a Vector3 instead of a Transform if you want a fixed value.

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

Follow this Question

Answers Answers and Comments

222 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 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 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 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 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 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 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 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

Need insight on strange (simple) 2D object transform bug 0 Answers

Locating Position of Object not Working? 2 Answers

Best way to detect if an object is near? 1 Answer

[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int 1 Answer

postion scale and rotation greyed out and not working 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