Search Unity

Transform values reseting after Start() call

Discussion in 'Scripting' started by smarcus, Feb 1, 2011.

  1. smarcus

    smarcus

    Joined:
    Sep 18, 2007
    Posts:
    113
    I'm seeing bizarre behavior when I instantiate an object. I can set values in Awake or Start, but they are reset immediately after Start is done. Observe:

    Code (csharp):
    1.  
    2. public class RacePlayer : MonoBehaviour {
    3.     public TubeRacer carPrefab;
    4.     private GameObject carHandle;
    5.     private TubeRacer car;
    6.    
    7.     public void Start()
    8.     {
    9.         carHandle = new GameObject("Race Handle");
    10.         car = (TubeRacer)GameObject.Instantiate(carPrefab,Vector3.zero,Quaternion.identity);
    11.         car.transform.parent=carHandle.transform;
    12.         car.transform.localPosition = new Vector3(0,-5f,0);
    13.         Debug.Log(car.transform.localPosition.y);
    14.         StartCoroutine(Report());
    15.     }
    16.    
    17.     IEnumerator Report()
    18.     {
    19.         yield return 0;
    20.         Debug.Log(car.transform.localPosition.y);
    21.         yield return 0;
    22.     }
    23. }
    24.  
    This script is the only object in the scene (and TubeRacer is an empty C# class that extends MonoBehavior), but this is my debug output:

    -5
    0

    and, indeed, the localPosition of the child object is 0's across the board.

    Why is this being reset? I have recompiled the project twice and built it from script up, and I'm still seeing this. I'm new to C#, but I know you can't set the .x, .y and .z parts of its localPosition manually... is something similar to that happening? Why does the Debug output work?


    Update: recompiling a third time fixed it. Odd.
     
    Last edited: Feb 1, 2011