• 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
1
Question by Mojova · Aug 14, 2015 at 11:58 AM · positionreset-position

Unity randomly resetting gameobjects position to world zero

I have a problem with some gameobjects randomly resetting their position to (0,0,0). Their position is correct during Awake and Start but in the first Update the position is reset. If I try to move the object, the position is immediately back to (0,0,0). This don't happen everytime I run the scene. Sometimes it might work dozens of times in a row and sometimes the bug appears many times in a row.

The objects have a SpriteRenderer, Rigidbody2D, PolygonCollider2D and a script attached to them. I don't move the position in the script so that should not be the problem. There is not errors shown in the console so I really have no clue what could cause this random behaviour. I'm using Unity 5.1.2 64-bit on a windows 7 machine. I built the game for android and the bug randomly appears also on android devices.

Here is the code I have in the script in case it helps.

     public Transform activationHeight;
 
     private Transform copterTransform;
     private Transform _transform;
     private Rigidbody2D _rigidbody;
     private bool inAction;
 
     void Awake() {
         Debug.Log ("Awake position: " + transform.position);
     }
 
     // Use this for initialization
     void Start () {
         Debug.Log ("Start position 1: " + transform.position);
         _transform = transform;
         _rigidbody = GetComponent<Rigidbody2D>();
         copterTransform = GameObject.Find("Copter").transform;
         Debug.Log ("Start position 2: " + _transform.position);
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log ("Position: " + _transform.position);
         if (copterTransform.position.y > activationHeight.position.y && inAction == false) {
             inAction = true;
             Debug.Log ("Activate");
             _transform.position = copterTransform.position + Vector3.right * 20 - activationHeight.localPosition;
             _rigidbody.velocity = -Vector3.right * 20;
         }
         if (inAction == true) { 
         
         }
     }
     void OnTriggerEnter2D(Collider2D other) {
         Copter copter = other.GetComponent<Copter>();
         if (copter != null)
             copter.Detonate();
     }
Comment
Add comment · Show 5
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 Wisearn · Aug 14, 2015 at 12:09 PM 0
Share

Possibly you need to do something like: _transform.position = ((copterTransform.position + Vector3.right) * 20) - activationHeight.localPosition;

If it might be doing the calculations in the wrong order?

Alternativly put a Debug.Log(copterTransform.position + Vector3.right * 20 - activationHeight.localPosition);

to see what it's setting your position to

and further

Debug.Log(activationHeight.localPosition); and Debug.Log(copterTransform.position);

to know where it's going wrong

avatar image Wisearn · Aug 14, 2015 at 12:11 PM 0
Share

Do you want to do

 (copterTransform.position + Vector3.right) * 20

or

 copterTransform.position + (Vector3.right * 20) ?

Possibly you need to make sure the calculations are in the right order like this.

Alternativly do

 Debug.Log(copterTransform.position + Vector3.right * 20 - activationHeight.localPosition);

and

 Debug.Log(copterTransform.position);
 Debug.Log(activationHeight.localPosition);

to know what numbers you're dealing with and what exactly is going wrong

avatar image Scribe · Aug 14, 2015 at 12:15 PM 0
Share

Do you have any animations acting on the object?

avatar image Mojova · Aug 14, 2015 at 04:23 PM 0
Share

The script has been working correctly before I updated to Unity 5.1.2. so the code should be correct. Before that I was using 5.0.2. The problem is that after the Start method has run, the gameobject will move to (0,0,0) coordinates in world space. If I move the object in inspector it will just go back to zero position.

This happens in the start of the scene but not everytime I open the scene.

avatar image Mojova · Aug 14, 2015 at 05:22 PM 0
Share

I also tried to remove the script from the object so it only had a SpriteRenderer, Rigidbody2D and PolygonCollider2D attached to it but still the object sometimes moves to the world center. No other script or gameobject references the gameobject so nothing should be moving it yet it still randomly appears in the wrong place.

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by Mojova · Aug 15, 2015 at 07:53 AM

I solved the problem. It was caused by the rigidbody2Ds position constraints. I confirmed this by having few clones of the gameobject put to the scene and ticked the constraint off from one of them.

When the bug appeared again, only the gameobjects with the position Y or X frozen was reseted to the (0,0,0) position. I ticked the constraint back on and that gameobject was also moved to the zero position after that.

I'm not sure if that is how the constraints are supposed to work or is it a bug. But anyway that was causing the random behaviour.

Comment
Add comment · Show 2 · 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 jc-drile77 · Oct 04, 2015 at 03:32 PM 0
Share

This has happened to me too. I dont know if its a bug or not. But im sure that in 3D constraints it doesnt happen. If someone can enlighten us I would be very happy. Btw I´m using unity 5.2

Edit: Its even worse than that, it makes every collision with a trigger work improperly.

avatar image StarShipMan · May 13, 2016 at 12:37 PM 0
Share

Thank you for the fix. This annoyed me for months until I put serious effort into fixing it. Your solution helped!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Resetting multiple objects position 1 Answer

My objects are not moving when collided with player when trying to reset their position after a timer. 0 Answers

My character's y position gets reset on play. 0 Answers

Camera rotation around player while following. 6 Answers

cant set button back to original size and position after running 2d animation 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