Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is there a way to return multiple variables?

Discussion in 'Scripting' started by SpyridonZ, Apr 8, 2010.

  1. SpyridonZ

    SpyridonZ

    Joined:
    Jun 7, 2009
    Posts:
    97
    I only know how to return a single variable per a single function.

    In JS, is there a way to use "return" on multiple variables in the same function?
     
  2. unitrix

    unitrix

    Joined:
    Mar 19, 2010
    Posts:
    279
    I think return an array/object
     
  3. radiolobito

    radiolobito

    Joined:
    Jun 12, 2009
    Posts:
    117
    and if i want to return two arrays?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Make a class that contains the elements you want, and return that. e.g., returning two ints:

    Code (csharp):
    1. class Stuff {
    2.     a : int;
    3.     b : int;
    4.     function Stuff (a : int, b : int) {
    5.         this.a = a;
    6.         this.b = b;
    7.     }
    8. }
    9.  
    10. function Foo () : Stuff {
    11.     return Stuff(1, 2);
    12. }
    --Eric
     
  5. Sirex

    Sirex

    Joined:
    Feb 7, 2011
    Posts:
    77
    Why do you want to return two values? Are you trying to handle errors?
     
  6. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    I kinda wish C# had this at times. The out parameter stuff seems so clunky.

    I use dictionaries a lot, and it bugs me.