Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Position only works on fresh object?

Discussion in '2D' started by Crlaozwyn, Feb 10, 2016.

  1. Crlaozwyn

    Crlaozwyn

    Joined:
    Jan 12, 2016
    Posts:
    13
    This problem is a bit convoluted and has taken me hours to even identify what is going on. I'm hoping someone here will be able to help me understand why so I can make it stop.

    I have a health bar that shifts to the left behind a mask as an enemy takes damage. This is done with a simple position command. Here's the weird behavior - if I close and reopen Unity then run the program, the bar disappears completely and has receives a position of -54.33333, -298.35. But, if I move the script to another game object and then back to the original one (or even just drag it back onto its own gameobject in the hierarchy), it receives the correct position of 0,0.

    What is this madness? Please help!
     
  2. Bibibis

    Bibibis

    Joined:
    Jul 28, 2015
    Posts:
    11
    Are you sure all your prefabs have a transform.position of (0,0,0)?
     
  3. Crlaozwyn

    Crlaozwyn

    Joined:
    Jan 12, 2016
    Posts:
    13
    No prefabs involved. Since it's part of the UI, I just have it in the scene. I literally move the script in the inspector to the same parent object in the hierarchy and it fixes the problem.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Would you mind sharing the relevant bits of your script?
     
  5. Crlaozwyn

    Crlaozwyn

    Joined:
    Jan 12, 2016
    Posts:
    13
    Update with solution in case anyone else runs into this madness:
    The issue was being caused by a timing issue. The function was being called before the Start where values were properly initialized. I moved where the first call happens and no more issues. Still not sure why moving the script onto the gameobject would change this, unless it messed with the behind-the-scenes hierarchy of when GameObjects are initialized into the scene.