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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Vector Value Resetting To (0,0,0) After Play Pressed

Discussion in 'Scripting' started by SecondDegreePerm, Aug 29, 2019.

  1. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    Hi All,

    I have an object containing a field called 'racerMidPoint' which has a value of (0,0.3,0) when in edit mode, but then when play mode is pressed this value resets to (0,0,0). I have confirmed this both through the inspector and debug messages. The field is private and has no setter, the only point the value is assigned to is in the 'RacerStart' function which is in turn only called in an editor script when an initialise button is pressed. Everything is initialised prior to the play button being pressed. As such there are no Start or Awake functions in any of my classes. Given these conditions, what could possibly be changing the value back to 0 when the play button is pressed?
    upload_2019-8-29_11-34-44.png

    Thanks in advance for the help,
    Dan
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It would be helpful if you posted the entire class (using code tags) instad just a single method as an Image. We don't know how you defined your attributes, and if/how you exposed them in Editor, if they are serialized or not. if/how you defined getters/Setters, and if the class inherits some funky methods.
     
  3. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    There is quite a bit of clutter in the entire class. Here are all of the references to the RacerMidPoint variable alongside the method calls to the only function which sets the value.
    Racer Class:
    Code (CSharp):
    1. [SerializeField]
    2.     private Vector3 racerMidPoint;
    Code (CSharp):
    1.  protected void RacerStart()
    2.     {
    3.         distanceTravelled = 0.01f;
    4.         lanePosition = currentPathSection.getCentreIndex();
    5.         Debug.Log("Local Transform " + transform.localPosition);
    6.         racerMidPoint = transform.localPosition;
    7.         Debug.Log("Racer MidPoint = "  + racerMidPoint);
    8.         currentPathCreator = currentPathSection.paths[lanePosition].pathCreator;
    9.         updateBottomTransform();
    10.     }
    Code (CSharp):
    1.  protected void Jump()
    2.     {
    3.         transform.localPosition += Vector3.up * jumpRate * Time.deltaTime;
    4.         jumpRate -= gravity;
    5.         if (transform.localPosition.y < racerMidPoint.y + groundHeight)
    6.         {
    7.             isInAir = false;
    8.             jumpRate = jump;
    9.             if (transform.localPosition.y != racerMidPoint.y + groundHeight)
    10.             {
    11.                 transform.localPosition = new Vector3(0, racerMidPoint.y + groundHeight, 0);
    12.             }
    13.         }
    14.     }
    15.  
    16.     protected bool AirCheck()
    17.     {
    18.         if (transform.localPosition.y > racerMidPoint.y)
    19.         {
    20.             if (!isColliding)
    21.             {
    22.                 return true;
    23.             }
    24.         }
    25.         return false;
    26.     }
    27.  
    28.     protected bool midPointInAcceptableRange(float delta)
    29.     {
    30.         Debug.Log("racer must be greater than " + (racerMidPoint.y + groundHeight - delta) + " and less than " + (racerMidPoint.y + groundHeight + delta) + " but actually is " + racerMidPoint.y);
    31.         return transform.localPosition.y > (racerMidPoint.y + groundHeight - delta) && transform.localPosition.y < (racerMidPoint.y + groundHeight + delta);
    32.     }

    Code (CSharp):
    1.     public Vector3 getRacerMidPoint()
    2.     {
    3.         return racerMidPoint;
    4.     }
    PlayerRacer Class (inherits from Racer)
    Code (CSharp):
    1.   public void playerRacerStart()
    2.     {
    3.         RacerStart();
    4.         Debug.Log(getRacerMidPoint());
    5.     }
    MapManager Class
    Code (CSharp):
    1. public void initialiseAllComponents()
    2.     {
    3.         foreach (MapSection s in map) {
    4.             s.initialise();
    5.         }
    6.         if (playerRacer != null) {
    7.             playerRacer.playerRacerStart();
    8.         }
    9.     }
    MapManagerEditor Class
    Code (CSharp):
    1. public override void OnInspectorGUI()
    2.     {
    3.         DrawDefaultInspector();
    4.  
    5.         MapManager map = (MapManager)target;
    6.  
    7.         if (GUILayout.Button("Initialise Map"))
    8.         {
    9.             map.initialiseAllComponents();
    10.         }
    11.     }
     
    Last edited: Aug 29, 2019
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    In your RacerStart method you have
    racerMidPoint = transform.localPosition;


    Could transform.localPosition be (0,0,0)? Maybe that's were the value is changing. I don't know when that method gets called, though.
     
  5. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    That is called when I press the button "Initialise Map", afterwards the inspector, and debugging, shows the value to be (0,0.3,0). Debugging also shows that the RacerStart is not called at any point after this. Within the prefab the local position is also (0,0.3,0). I am really at a loss as to what could be causing this!
     
  6. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    Is there perhaps some odd unity editor script quirk not letting this value persist in play mode? I cannot see any issue within the code itself
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Try remove the line above that @kdgalla identified... does your problem go away?

    Alternately, turn it into a getter/setter pair with a private backing store (like private Vector3 _racerMidPoint; ) and then put a breakpoint in the setter and attach the debugger. When it hits you will instantly know who the offender is by studying the callstack in the debugger.

    Also, those Debug.Log() statements each output a visible callstack too, in the log, but if somewhere ELSE is touching the code, that won't help.
     
  8. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    I removed that line, manually set the value to (0,0.3,0) in the inspector, then pressed play. The vector value is again then reset to (0,0,0).

    I believe you wanted me to do something like the below?

    Code (CSharp):
    1.     private Vector3 _racerMidPoint;
    2.     public Vector3 racerMidPoint { get { return _racerMidPoint; } private set { _racerMidPoint = value; Debug.Log("I'M BEING CHANGED!!"); } }
    Here's the output when i initialise the map in the editor:

    upload_2019-8-30_7-10-11.png

    Here's the very first line of output (refering to the value of racerMidPoint) when I press play
    upload_2019-8-30_7-11-35.png

    There was no call to the setter. I feel like this means there's something going on in the editor? Because I cannot see what is happening in code. Or it's something about editor scripts I'm unfamiliar with.
     
  9. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    Just giving this a bump
     
  10. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    So now its working? I really don't know why? I changed this line of code
    Code (CSharp):
    1. protected void RacerStart()
    2.     {
    3.         distanceTravelled = 0.01f;
    4.         lanePosition = currentPathSection.getCentreIndex();
    5.         Debug.Log("Local Transform " + transform.localPosition);
    6.         racerMidPoint = transform.localPosition;
    7.         Debug.Log("Racer MidPoint = "  + racerMidPoint);
    8.         currentPathCreator = currentPathSection.paths[lanePosition].pathCreator;
    9.         updateBottomTransform();
    10.     }
    so that
    Code (CSharp):
    1. distanceTravelled = 0.00f;
    and then it started working. I decided to investigate why this may be the case, assuming that some logic must change with a starting value of 0.00f instead of 0.01f. I couldnt figure out why, so I decided to change it back to 0.01f, and then it still worked! Back to square one, but with the script now working? For some reason? I'm glad its fixed, but it disconcerts me somewhat that I have no idea why!
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    I'm not sure of your exact setup scene-wise (or prefab-wise) but I'm going to guess that it was a stored serialized value in your scene or prefab that was overriding what you set in code, either directly or through the transform values.

    If you clone the relevant part of your scene to a fresh scene, you might be able to get some insight. Keep in mind that if you have code that says:

    Code (csharp):
    1. public int foo = 1234;
    And then change it in the inspector, by the time the object is ready for action when you press PLAY, the value will NOT be 1234 but rather whatever you changed it to in the inspector, as Unity will deserialize that into your public field. This can be confusing at first but it is very predictable and makes sense once you understand how Unity loads/saves the scene every time you enter/leave play mode.
     
  12. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    I was checking the inspector value before pressing play, and it was (0,0.3,0). This value was not changed to (0,0.3,0) by me directly, it was changed by an editor script button which I pressed to initialise the scene. This is why I was wondering whether there is any difference between me physically entering the value into the inspect and pressing a button so that a editor script edits the value for me.

    Though this is generally useful knowledge!
     
    Last edited: Aug 31, 2019