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();
}