Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 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:
    717
    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,168
    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:
    485
    KelsoMRK likes this.
  4. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    717
    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:
    721
    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,168
    I'm not sure I understand what you're trying to do.
     
  7. kilik128

    kilik128

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

    KelsoMRK

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

    kilik128

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

    kilik128

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

    Suddoha

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

    kilik128

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

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    1,422
    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:
    717
    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,422
    Set the value with a method.