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. Dismiss Notice

Please explain return to me

Discussion in 'Getting Started' started by jsmall81, Jan 30, 2021.

  1. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    What does this do? If I declare a variable like this;

    Public Targetable target; (target able is another class not in this one)

    And then have a method;

    Public GetTarget()
    {return target;}

    And I call this from a separate script. Is it just referencing this class? So, a reference to Target able? In language terms, what's it "returning"? Can I set this to be an enum to be returned? So, four options in an enum in a script. Then I have one option to be "stone". I set it to stone in an if statement. Can I have that enum returned to another script?
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    return
    , well, "returns" a response from a method.
    In your example, yes, it's returning the reference of the "Targetable" variable.
    And yes, you can return things from other classes, so if a different script calls this method, it will have this script's "Targetable" reference.

    Yes, a method can return anything - you just need to define the type that it returns.

    You're probably already familiar with
    void
    , right? That just means the method doesn't return anything after being executed. You can change
    void
    to any other type that you want a method to return:
    Code (CSharp):
    1. //This method returns nothing.
    2. void Method1() { }
    3.  
    4. //This method returns a string.
    5. string Method2() { }
    6.  
    7. //This method returns a float.
    8. float Method3() { }
    9.  
    10. //This method returns a Vector3.
    11. Vector3 Method4() { }
    12.  
    13. //This method returns a GameObject.
    14. GameObject Method5() { }
    15.  
    16. //This method returns a Renderer.
    17. Renderer Method6() { }
    18.  
    19. //etc...
    Note that the
    return
    keyword will immediately stop a method's execution, so if you have something like this, for instance:
    Code (CSharp):
    1. float Add(float num1, float num2) {
    2.    float result = num1 + num2;
    3.    return result;
    4.  
    5.    Debug.Log("Result is: " + result);
    6. }
    The
    Debug.Log
    statement here (and anything after it) will never be run.

    A method can also return another method, and all that means is that it returns the result of whatever that other method returns:
    Code (CSharp):
    1. float Calculate(float num1, float num2) {
    2.    //This method simply returns what the "Add" method returns.
    3.    return Add(num1, num2);
    4. }
    5.  
    6. float Add(float num1, float num2) {
    7.    float result = num1 + num2;
    8.    return result;
    9. }
    If a method returns an object, any other object-type that inherits from that object can be returned as well.
    For example, Unity's
    BoxCollider
    ,
    SphereCollider
    ,
    CapsuleCollider
    and other colliders all inherit from the
    Collider
    class.
    So if a method returns a
    Collider
    type, any type of collider can be returned:
    Code (CSharp):
    1. public class Example : MonoBehavior {
    2.    public BoxCollider box;
    3.    public SphereCollider sphere;
    4.    public CapsuleCollider capsule;
    5.  
    6.    public Collider GetColliderByIndex(int index) {
    7.       if(index == 0) {
    8.          return box;
    9.       }
    10.       else if(index == 1) {
    11.          return sphere;
    12.       }
    13.       else if(index == 2) {
    14.          return capsule;
    15.       }
    16.       else {
    17.          return null;
    18.       }
    19.    }
    20. }
    Finally, you can also use the
    return
    keyword in
    void
    methods as well if you wish to stop their execution. For example:
    Code (CSharp):
    1. void Jump(float stamina) {
    2.    if(stamina < 25f) {
    3.       //Cannot jump if stamina is too low. Just stop the method immediately.
    4.       return;
    5.    }
    6.  
    7.    //The code down here will only be run if "stamina" is greater than 25f.
    8.    transform.position = //etc...
    9. }
     
    Last edited: Jan 31, 2021
    JoNax97 and Schneider21 like this.
  3. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Ok, that's awesome. Thank you.