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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Part of my if-statement doesnt run properly in Update

Discussion in 'Scripting' started by boooels, Dec 4, 2018.

  1. boooels

    boooels

    Joined:
    Oct 26, 2018
    Posts:
    31
    So i got this if statement in my script:

    Code (CSharp):
    1.  public void Update()
    2.     {
    3.         if (Input.GetButtonDown(triggerButton) && Time.time > nextFire && resourceBar.currentEnergy >= abilityCost && ballSpeed.player2LastCollision == true)
    4.         {
    5.             nextFire = Time.time + basisCooldown.cooldown;
    6.  
    7.             Cost();
    8.  
    9.             ballRB2D = ball.GetComponent<Rigidbody2D>();
    10.  
    11.             int ballPos = Random.Range(0, 3);
    12.  
    13.             //Real ball spawned on top
    14.             if (ballPos == 0)
    15.             {
    16.                 ballRB2D.AddForce(new Vector2(-1, positiveAngle) * (ballSpeed.currSpeed / 100000), ForceMode2D.Impulse);
    17.                 CreateIllusions(Vector2.left.normalized);
    18.                 CreateIllusions(new Vector2(-1, negativeAngle));
    19.             }
    20.  
    21.             //Real ball spawned in mid
    22.             else if (ballPos == 1)
    23.             {
    24.                 CreateIllusions(new Vector2(-1, positiveAngle));
    25.                 ballRB2D.AddForce(Vector2.left.normalized * (ballSpeed.currSpeed / 100000), ForceMode2D.Impulse);
    26.                 CreateIllusions(new Vector2(-1, negativeAngle));
    27.             }
    28.  
    29.             //Real ball spawned at bottom
    30.             else
    31.             {
    32.                 CreateIllusions(new Vector2(-1, positiveAngle));
    33.                 CreateIllusions(Vector2.left.normalized);
    34.                 ballRB2D.AddForce(new Vector2(-1, negativeAngle) * (ballSpeed.currSpeed / 100000), ForceMode2D.Impulse);
    35.             }
    36.         }
    37.     }
    This is supposed to run the entire thing once i press my triggerButton. The issue is it seems to not include my "Cost();". On the first click its runs everything but the "Cost();" function. Now this should make me unable to activate the if statement againt for another 10 seconds or so. But instead i can press the button again to ONLY run the "Cost();" function it seems. After this event sometimes only the Cost(); function runs, and sometimes the same scenario as the first one happens.
    This is the Cost function if youre wondering:
    Code (CSharp):
    1. public void Cost()
    2.     {
    3.         //Set needed variables
    4.         resourceBar.abilityCost = abilityCost;
    5.         resourceBar.p1TriggerButton1 = triggerButton;
    6.     }
    I want to point out that i have a script almost exactly the same as this one, the only difference between them is which gameobjects are being called in start. And that script works perfectly fine.
    ANY help would really be appreciated, ive been stuck on this the entire day. Thanks for your time :)
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Do you have any console errors?
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Add plenty of Debug.Log() to see exactly which parts of your code is executed.
    To me, it seems odd to set the ability cost only after you press fire
    verify that basisCooldown.cooldown is 10
     
  4. boooels

    boooels

    Joined:
    Oct 26, 2018
    Posts:
    31
    I have verified that the basisCooldown is exactly 10 :)
    I have also used a debug everywhere i would need to, and it seems to run it all, just not the way its supposed to.
    Code with Debug:
    Code (CSharp):
    1. public void Update()
    2.     {
    3.         if (Input.GetButtonDown(triggerButton) && Time.time > nextFire && resourceBar.currentEnergy >= abilityCost && ballSpeed.player2LastCollision == true)
    4.         {
    5.             Debug.Log("1");
    6.             nextFire = Time.time + basisCooldown.cooldown;
    7.             Debug.Log("2");
    8.             Cost();
    9.             Debug.Log("3");
    10.             ballRB2D = ball.GetComponent<Rigidbody2D>();
    11.             Debug.Log("4");
    12.             int ballPos = Random.Range(0, 3);
    13.  
    14.             //Real ball spawned on top
    15.             if (ballPos == 0)
    16.             {
    17.                 ball.transform.Translate(new Vector2(1, .5f) * ballSpeed.currSpeed * Time.deltaTime);
    18.                 //ballRB2D.AddForce(new Vector2(1, positiveAngle) * (ballSpeed.currSpeed / 2), ForceMode2D.Impulse);
    19.                 CreateIllusions(Vector2.right.normalized);
    20.                 CreateIllusions(new Vector2(1, -.5f));
    21.             }
    22.  
    23.             //Real ball spawned in mid
    24.             else if (ballPos == 1)
    25.             {
    26.                 CreateIllusions(new Vector2(1, .5f));
    27.                 ball.transform.Translate(Vector2.right.normalized * ballSpeed.currSpeed * Time.deltaTime);
    28.                 //ballRB2D.AddForce(Vector2.right.normalized * (ballSpeed.currSpeed / 2), ForceMode2D.Impulse);
    29.                 CreateIllusions(new Vector2(1, -.5f));
    30.             }
    31.  
    32.             //Real ball spawned at bottom
    33.             else
    34.             {
    35.                 CreateIllusions(new Vector2(1, .5f));
    36.                 CreateIllusions(Vector2.right.normalized);
    37.                 ball.transform.Translate(new Vector2(1, -.5f) * ballSpeed.currSpeed * Time.deltaTime);
    38.                 //ballRB2D.AddForce(new Vector2(1, negativeAngle) * (ballSpeed.currSpeed / 2), ForceMode2D.Impulse);
    39.             }
    40.             Debug.Log("5");
    41.         }
    42.     }
    43.  
    44. public void Cost()
    45.     {
    46.         //Set needed variables
    47.         resourceBar.abilityCost = abilityCost;
    48.         resourceBar.p1TriggerButton1 = triggerButton;
    49.         Debug.Log("6");
    50.     }
    The debug in the consol says the following: 1, 2, 6, 3, 4, 5. As i would expect...

    Here is the other similar script for comparison... this one works 100% as i intend it to:
    Code (CSharp):
    1.  public float nextFire;
    2.     public float abilityCost = 7;
    3.  
    4.     public string triggerButton = "P1Ability1";
    5.  
    6.     public GameObject Illusion;
    7.  
    8.     private GameObject ball;
    9.  
    10.     private Rigidbody2D illusionRB2D;
    11.     private Rigidbody2D ballRB2D;
    12.  
    13.     private BallControl ballSpeed;
    14.     private BasisCooldown basisCooldown;
    15.     [SerializeField]
    16.     private ResourceBar resourceBar;
    17.  
    18.     public void Start()
    19.     {
    20.         ball = GameObject.FindGameObjectWithTag("Ball");
    21.  
    22.         resourceBar = gameObject.GetComponent<ResourceBar>();
    23.         basisCooldown = gameObject.GetComponent<BasisCooldown>();
    24.         ballSpeed = ball.GetComponent<BallControl>();
    25.     }
    26.  
    27.     public void Update()
    28.     {
    29.         if (Input.GetButtonDown(triggerButton) && Time.time > nextFire && resourceBar.currentEnergy >= abilityCost && ballSpeed.player1LastCollision == true)
    30.         {
    31.             nextFire = Time.time + basisCooldown.cooldown;
    32.  
    33.             Cost();
    34.  
    35.             ballRB2D = ball.GetComponent<Rigidbody2D>();
    36.  
    37.             int ballPos = Random.Range(0, 3);
    38.  
    39.             //Real ball spawned on top
    40.             if (ballPos == 0)
    41.             {
    42.                 ball.transform.Translate(new Vector2(1, .5f) * ballSpeed.currSpeed * Time.deltaTime);
    43.                 //ballRB2D.AddForce(new Vector2(1, positiveAngle) * (ballSpeed.currSpeed / 2), ForceMode2D.Impulse);
    44.                 CreateIllusions(Vector2.right.normalized);
    45.                 CreateIllusions(new Vector2(1, -.5f));
    46.             }
    47.  
    48.             //Real ball spawned in mid
    49.             else if (ballPos == 1)
    50.             {
    51.                 CreateIllusions(new Vector2(1, .5f));
    52.                 ball.transform.Translate(Vector2.right.normalized * ballSpeed.currSpeed * Time.deltaTime);
    53.                 //ballRB2D.AddForce(Vector2.right.normalized * (ballSpeed.currSpeed / 2), ForceMode2D.Impulse);
    54.                 CreateIllusions(new Vector2(1, -.5f));
    55.             }
    56.  
    57.             //Real ball spawned at bottom
    58.             else
    59.             {
    60.                 CreateIllusions(new Vector2(1, .5f));
    61.                 CreateIllusions(Vector2.right.normalized);
    62.                 ball.transform.Translate(new Vector2(1, -.5f) * ballSpeed.currSpeed * Time.deltaTime);
    63.                 //ballRB2D.AddForce(new Vector2(1, negativeAngle) * (ballSpeed.currSpeed / 2), ForceMode2D.Impulse);
    64.             }
    65.         }
    66.     }
    67.  
    68.     public void CreateIllusions(Vector2 angleOffset)
    69.     {
    70.         GameObject illusions = Instantiate<GameObject>(Illusion);
    71.         illusions.transform.position = ball.transform.position;
    72.  
    73.         illusionRB2D = illusions.GetComponent<Rigidbody2D>();
    74.         illusionRB2D.AddForce(angleOffset * (ballSpeed.currSpeed), ForceMode2D.Impulse);
    75.     }
    76.  
    77.      public void Cost()
    78.      {
    79.         //Set needed variables
    80.         resourceBar.abilityCost = abilityCost;
    81.         resourceBar.p1TriggerButton1 = triggerButton;
    82.      }
    And here is the Entire script where the issue is, as you can see the difference between them is almost nothing:
    Code (CSharp):
    1. public float nextFire;
    2.     public float abilityCost = 7;
    3.     public float positiveAngle = 1;
    4.     public float negativeAngle = -1;
    5.  
    6.     public string triggerButton = "P2Ability1";
    7.  
    8.     public GameObject Illusion;
    9.  
    10.     private GameObject ball;
    11.  
    12.     private Rigidbody2D illusionRB2D;
    13.     private Rigidbody2D ballRB2D;
    14.  
    15.     public BallControl ballSpeed;
    16.     private BasisCooldown basisCooldown;
    17.     [SerializeField]
    18.     private ResourceBarP2 resourceBar;
    19.  
    20.     public void Start()
    21.     {
    22.         ball = GameObject.FindGameObjectWithTag("Ball");
    23.  
    24.         resourceBar = gameObject.GetComponent<ResourceBarP2>();
    25.         basisCooldown = gameObject.GetComponent<BasisCooldown>();
    26.         ballSpeed = ball.GetComponent<BallControl>();
    27.     }
    28.  
    29.     public void Update()
    30.     {
    31.         if (Input.GetButtonDown(triggerButton) && Time.time > nextFire && resourceBar.currentEnergy >= abilityCost && ballSpeed.player2LastCollision == true)
    32.         {
    33.             Debug.Log("1");
    34.             nextFire = Time.time + basisCooldown.cooldown;
    35.             Debug.Log("2");
    36.             Cost();
    37.             Debug.Log("3");
    38.             ballRB2D = ball.GetComponent<Rigidbody2D>();
    39.             Debug.Log("4");
    40.             int ballPos = Random.Range(0, 3);
    41.  
    42.             //Real ball spawned on top
    43.             if (ballPos == 0)
    44.             {
    45.                 ball.transform.Translate(new Vector2(1, .5f) * ballSpeed.currSpeed * Time.deltaTime);
    46.                 //ballRB2D.AddForce(new Vector2(1, positiveAngle) * (ballSpeed.currSpeed / 2), ForceMode2D.Impulse);
    47.                 CreateIllusions(Vector2.right.normalized);
    48.                 CreateIllusions(new Vector2(1, -.5f));
    49.             }
    50.  
    51.             //Real ball spawned in mid
    52.             else if (ballPos == 1)
    53.             {
    54.                 CreateIllusions(new Vector2(1, .5f));
    55.                 ball.transform.Translate(Vector2.right.normalized * ballSpeed.currSpeed * Time.deltaTime);
    56.                 //ballRB2D.AddForce(Vector2.right.normalized * (ballSpeed.currSpeed / 2), ForceMode2D.Impulse);
    57.                 CreateIllusions(new Vector2(1, -.5f));
    58.             }
    59.  
    60.             //Real ball spawned at bottom
    61.             else
    62.             {
    63.                 CreateIllusions(new Vector2(1, .5f));
    64.                 CreateIllusions(Vector2.right.normalized);
    65.                 ball.transform.Translate(new Vector2(1, -.5f) * ballSpeed.currSpeed * Time.deltaTime);
    66.                 //ballRB2D.AddForce(new Vector2(1, negativeAngle) * (ballSpeed.currSpeed / 2), ForceMode2D.Impulse);
    67.             }
    68.             Debug.Log("5");
    69.         }
    70.     }
    71.  
    72.     public void CreateIllusions(Vector2 angleOffset)
    73.     {
    74.         GameObject illusions = Instantiate<GameObject>(Illusion);
    75.         illusions.transform.position = ball.transform.position;
    76.  
    77.         illusionRB2D = illusions.GetComponent<Rigidbody2D>();
    78.         illusionRB2D.AddForce(angleOffset * (ballSpeed.currSpeed / 10000), ForceMode2D.Impulse);
    79.     }
    80.  
    81.     public void Cost()
    82.     {
    83.         //Set needed variables
    84.         resourceBar.abilityCost = abilityCost;
    85.         resourceBar.p1TriggerButton1 = triggerButton;
    86.         Debug.Log("6");
    87.     }
    And do you find it weird to use ability cost after triggering? im curious
    Sorry for the wall of text
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    both scripts set p1TriggerButton1 in Cost(), should the one using P2Ability1 be using something like 'p2TriggerButton1'

    Instead of just logging a sequence of numbers, try logging the value of variables to make sure they are what you think they are (eg. the value of nextFire before and after it is modified)
     
    boooels likes this.