Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How to prevent inspector from overwritting the transform.position value at start?

Discussion in 'Scripting' started by unity_9C73F69DA8E4AE188ADA, Jun 15, 2023.

  1. unity_9C73F69DA8E4AE188ADA

    unity_9C73F69DA8E4AE188ADA

    Joined:
    May 5, 2023
    Posts:
    2
    Hello,

    I’m facing a difficulty setting transform.position at Start(). The value is overwritten by inspector value of the Transform. The transform.position = (…) assignment is checked with Debug.Log(…) at the end of Start() hook and shows the right value. It’s overwritten by inspector value (I’ve checked different values and nothing else overwrites that).
    The GameObject has simple Rigidbody forces-based controller (no Character Controller) and an Agent script (setting the position in Agent’s Initialize() is also futile.

    Code (CSharp):
    1.    private void Start() {
    2.        transform.position = new Vector3(0, flightSettings.altitudeStart, 0);
    3.        Debug.LogWarning(transform.position);
    4.    }
    flightSettings is a ScriptableObject passed by inspector.

    The reassignment works fine later in the handler of Reset event fired by a collision.

    Code (CSharp):
    1.    public void OnReset(bool isStart)
    2.    {
    3.        rb.velocity = new Vector3();
    4.        rb.angularVelocity = new Vector3();
    5.        transform.rotation = Quaternion.identity;
    6.        transform.position = new Vector3(0, flightSettings.altitudeStart, 0);
    7.        rb.AddForce(transform.forward * 30, ForceMode.VelocityChange);
    8.        roll = 0;
    9.        pitch = 0;
    10.        yaw = 0;
    11.        throttle = 0;
    12.    }
    Firing the event on Start() still end in overwritting value.

    What should I do?

    Greetings
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    It's probably not being overwritten by the inspector, most likely its happening because of the rigidbody.

    If rigidbody's are involved you should just
    rb.MovePosition(Vector3.zero)
    , though updates to physics objects should happen in FixedUpdate preferably.
     
  3. unity_9C73F69DA8E4AE188ADA

    unity_9C73F69DA8E4AE188ADA

    Joined:
    May 5, 2023
    Posts:
    2
    You're one smart dragon, aren't you

    Thank you very much. It works fine :)
    They do. Only reset on ML episode beginning is happening outside.
     
    spiney199 likes this.