Search Unity

Update method called twice

Discussion in 'Scripting' started by ShardsOfPast, Aug 26, 2015.

  1. ShardsOfPast

    ShardsOfPast

    Joined:
    Aug 25, 2015
    Posts:
    4
    Hey,

    I ran in a bit of an issue with the CharacterController. The update method is called twice, what causes problems with controlling the character. This is the shortest amount of code to cause this problem on my computer.

    I have my settings for the CharacterController like this: Center is at 0, 0.95, 0; Radius is 0.4; Height is 1.85; The character is slightly above the ground so it can't stick into it. Now if you'll apply this script to the GameObject and you'll run the scene, the debug console is telling you False and True. If you collapse the console output you see that False and True are added at nearly the same time, which means for me, in the same frame (There is only a really short delay)!
    Adding a bool wasCalled into the class and checking if the update-method was already called makes the program really laggy, so that's not a solution.
    Also I have no code affecting the script, this is a test script I'm using parallel to my real PlayerController, and yes, the real one is disabled!

    Code (CSharp):
    1.  
    2. [RequireComponent (typeof (CharacterController))]
    3. public class PlayerController2 : MonoBehaviour
    4. {
    5.     private CharacterController cc;
    6.     private float verticalVelocity = 0;
    7.  
    8.     void Start()
    9.     {
    10.         this.cc = GetComponent<CharacterController> ();
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         transform.Rotate (0, Input.GetAxis ("Mouse X") * UserPreferences.mouseSensitivity, 0);
    16.  
    17.         Debug.Log(cc.isGrounded);
    18.  
    19.         if (cc.isGrounded)
    20.         {
    21.             this.verticalVelocity = 0;
    22.         }
    23.         else this.verticalVelocity = -1;
    24.  
    25.         this.cc.Move(transform.rotation * new Vector3 (Input.GetAxis ("Horizontal") * 3, this.verticalVelocity,
    26.                                                        Input.GetAxis ("Vertical") * 3) * Time.deltaTime);
    27.     }
    28. }
    29.  
    I hope anyone could help me,

    SOP.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    Update is called once per frame.

    FYI you don't need 'this.something = foo'. You really only need it when you have ambiguity between member names.

    Whats probably happening is cc.isGrounded or verticalVelocity are oscillating, or cc.isGrounded is unreliable - eg the collider is falling through the floor or something.
     
    ShardsOfPast and Kiwasi like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm with @LaneFox on this one. Your grounded check might be buggy.

    The other thing to check is that you haven't accidentally added the script twice or to two different GameObjects. Debug.Log will actually take a second parameter. If you set it up like this you can quickly verify that both the true and false are coming from the same object.

    Code (CSharp):
    1. Debug.Log(cc.isGrounded, this);
     
    ShardsOfPast and LaneFox like this.
  4. ShardsOfPast

    ShardsOfPast

    Joined:
    Aug 25, 2015
    Posts:
    4
    First, thanks for your answers.
    The script is only applied once and now I'm sure that my collider is causing this issue. Setting the GameObject 1 unit higher makes it fall and only print out "false". If it collides with my "map" (It's just a flat mesh) the console shows me both, true and false.

    Any ideas how to fix this?
     
  5. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Just in case you hadn't tried this before - if you need to step through one frame at a time (which would let you see if indeed something is called multiple times in one frame, which update shouldn't be) you can pause the game in the editor, then hit that step through button at the top next to play and pause button. Comes in handy all the time!