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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Little Update function troubleshoot ;

Discussion in 'Scripting' started by Capollastre, Aug 17, 2015.

  1. Capollastre

    Capollastre

    Joined:
    Jul 15, 2014
    Posts:
    8
    Hello guys,
    I am getting mad about a problem that is compromising my scripting behaviour.

    this simple code under Camera object;

    Code (CSharp):
    1. void Update () {
    2.  
    3. if (activarotacionI == true) {
    4.        
    5.             activarotacionI = false;
    6.             activarotacionD = false;
    7.             float deltaAngle = 90f;
    8.             Debug.Log ("ein");
    9. }
    yes the fact is that i was trying to rotate the camera in this script but i noticed the angles being rotated weren't being calculated good. So the point is : when i execute code above I get 3 "ein" results....... Its like frame 1 , 2 and 3 where launched simultaneous and don't even noticed i switched the if statement off. As far i am concerned the Frame 1 Update function is done completely before Frame 2 Update function start... so I am expecting just ONE " ein " result and i don't get why I got three of them....... I posted this simple question because i forgot smething that seems very important on the code part of Unity and maybe this could help any other newbie coders.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Have you applied the component 3 times in the scene? 3 different objects, or one object 3 times
     
  3. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Oh no....................................

    Code (CSharp):
    1.     void Update () {
    2.    
    3.     if (activarotacionI == true) {
    4.          
    5.                 activarotacionI = false;
    6.                 activarotacionD = false;
    7.                 float deltaAngle = 90f;
    8.                 Debug.Log ("ein");
    9.  
    10.     }
    11.  
    You are missing a }

    That if statement is empty at the moment. The compiler sees the lines under the if statement as just normal commands to be executed everyframe.

    Change the code to:

    Code (CSharp):
    1.     void Update () {
    2.    
    3.     if (activarotacionI == true) {
    4.          
    5.                 activarotacionI = false;
    6.                 activarotacionD = false;
    7.                 float deltaAngle = 90f;
    8.                 Debug.Log ("ein");
    9.     }
    10. }
    11.  


     
  4. Capollastre

    Capollastre

    Joined:
    Jul 15, 2014
    Posts:
    8
    hello , i just solved and very sorry about you losing the time...... I am saying the problem and the solution if any new coders uses as a tip.
    The problem was what i told at the first post.
    The thing is that the "activarotacionI" was fired to "true" by a button press... be aware guys of a button press or whatever action performed under update function, because it will run over and over until released. Counting 200fps a Left click can perform tons of update actions.
    I just brute forced it by :
    Code (CSharp):
    1. if (Input.GetKey (KeyCode.LeftArrow)) {
    2.             if (flying == false){
    3.                 flying = true;
    4.        
    5.             activarotacionI = true;
    6.  
    7.             }
    8.                 }
    And also in the update function i updated the rotation by this :

    Code (CSharp):
    1. if (activarotacionI == true) {
    2.             //enabled = false;
    3.             deltaAngle = Time.deltaTime * 190f;
    4.             activarotacionD = false;
    5.  
    6.             if ( currentRotationAngle > 90f )
    7.             {
    8.                 activarotacionI = false;
    9.                 deltaAngle = 90f - currentRotationAngle;
    10.                 flying = false;
    11.             }
    12.             transform.RotateAround (Vector3.zero,Vector3.up, deltaAngle);
    13.             lightus.transform.RotateAround(Vector3.zero,Vector3.up,deltaAngle);
    14.             currentRotationAngle = currentRotationAngle + deltaAngle;
    15.  
    16.             Debug.Log (currentRotationAngle);
    17.             if (currentRotationAngle == 90f){
    18.                 currentRotationAngle = 0;
    19.             }
    20.         }
    everything seems work now ! thank you !