Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

return ref out variable

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

  1. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    726
    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,438
    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:
    820
    KelsoMRK likes this.
  4. kilik128

    kilik128

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

    kilik128

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

    KelsoMRK

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

    kilik128

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

    kilik128

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

    Suddoha

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

    kilik128

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

    Suddoha

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