How to view the return value of a method without re-executing it?


  • Hi, so I'm writing this with the unity framework but this is more of a general programming question. 

    Attack() returns either a value of x or it returns 0. I want to call Attack() to be used in another method and then see whether or not it returned 0. Currently what I have is I call Attack() and then call it again (rather nonsensically) to see if it equals zero. So i'd be working with two potentially completely different results. 

    private void Engage()
            attackSpeed -= Time.deltaTime;
            //After x amount of time, apply damage.
            if (attackSpeed <= 0)
                //get the leader's healthbar component
                health = getLeader.Leader().GetComponent<HealthBar>();
                //I want too see the value returned from Attack() without calling it.
                if (Attack() == 0.0f)
                    missed = true;
                else { missed = false; }
                //reset timer
                attackSpeed = 2.0f;
    How do I call the Attack() method and check it's value to see if it returned zero?
    Thursday, March 30, 2017 8:58 PM


  • Capture the returned value in a variable. Then reuse the variable.

    var attack = Attack();
    if (attack == 0.0f)

    Michael Taylor

    • Marked as answer by FMC Q Thursday, March 30, 2017 9:33 PM
    Thursday, March 30, 2017 9:07 PM