Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

return ref out variable

Discussion in 'Scripting' started by kilik128, Sep 13, 2017.

  1. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    718
    hi i try make fonction for return variable

    for exemple

    void ref int check ( ){

    return ref variable;

    }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,356
    ref is an argument keyword. You pass a primitive to a method as a reference, not return one. What it is you're trying to do?
     
  3. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    612
    KelsoMRK likes this.
  4. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    718
    ok thank's

    i try found the easy way to get variable from class via on inspector and go back with him in another fonction


    what's the unity c# version for information ?
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    963
    remember your return type is the first word after public or private, so if you put
    void myfunction(){}

    It means there is no return.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,356
    I'm not sure I understand what you're trying to do.
     
  7. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    718
    just select variable in inspector by enum or reflexion
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,356
    What does "select" mean?
     
  9. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    718
    one selection like delegate fonction
     
  10. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    718
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    1,598
    There are probably better ways to do it instead of breaking encapsulation in such a way.
     
  12. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    718
    yes any idea please ?
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    1,598
    Yes, use normal OOP techniques. Getter/Setter would be enough for that, I don't see any benefit from your choice using the ref-keyword in returns (besides that it's not officially supported any way), as it just breaks the encapsulation without any further advantage.

    If you've got a reference to the object that you'd call your "Cool()" - method on, you could as well just call "SetXYZ(int value)" and the instance would still be in control of the internal logic, validation and assigment. The changes would aswell be reflected in the inspector.

    Besides that, your int variable is public anyway. This is kinda redundant to having the Cool() method, as you could manipulate it directly.

    The ref-return feature has got its usages, but this is apparently none of the ones you'd use it for. It's just a way to reference values you don't want to copy all the time directly and IMO you shouldn't expose such a functionality in any way, because literally everyone calling this function and using the result as 'ref local' is able change the state from the outside and corrupt an object's state. It's just evil if used incorrectly.
     
  14. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    718
    sure without encapsulation advantage is less fun.

    what's you call ?
    "SetXYZ(int value)" and the instance would still be in control of the internal logic
     
  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    1,598
    Set the value with a method.