Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How to enable c# 7?

Discussion in '2018.3 Beta' started by User340, Sep 13, 2018.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Is there a setting somewhere? This code does not compile:
    Code (CSharp):
    1. transform.position.x = 5;
     
  2. MechEthan

    MechEthan

    Joined:
    Mar 23, 2016
    Posts:
    166
    C# 7 won't make that code compile ...

    Try something like this to make sure C# 7.2 is working:
    Code (CSharp):
    1. public static void NonMutatingMethod(in Vector3 v1)
    2. {
    3.     Debug.Log(v1.ToString());
    4. }
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Thanks for the tip. I was under the wrong assumption. Turns out C# 7 was actually enabled for me.
     
  4. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Is this supposed to work now with the new Roslyn compiler?
    What did I miss? was the Transform class rewritten to return position struct by reference or something?
     
  5. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    C# 7 enables you to declare ref-returns:
    Code (CSharp):
    1. public ref Vector3 Foo { get { return ref m_Foo; } };
    that can be used like you want
    Code (CSharp):
    1. whatefer.Foo.x = 3;
    it does NOT automagically convert unity API into ref-returns (i.e. transform.position is still by value)

    for that, they first need to remove the old runtime and require C# 7 to be mandatory, then implement it. and only if they decide it's worth it and doesn't break existing stuff
     
    LeonhardP likes this.
  6. one_one

    one_one

    Joined:
    May 20, 2013
    Posts:
    621
    Mutable/referenced structs can cause a lot of headaches. Plus, a lot of unity-related code strongly relies on the built-in structs like Vector3, Quaternion, Matrix4x4 not to be references.
     
    xVergilx likes this.
  7. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I hate to say it, but that was one true benefit of UnityScript.
     
  8. illuminati_systemd_NWO

    illuminati_systemd_NWO

    Joined:
    Mar 30, 2014
    Posts:
    6
    If you open project solution file using i.e. notepad and check <LangVersion>, it will show you 7.2, so I think it should be enabled.
     
  9. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    And probably only.
     
    hippocoder likes this.
  10. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    Hi all i need help please

    i got this

    Code (CSharp):
    1.  
    2.     public int Ready = 0;
    3.  
    4.  
    5. public ref int Got(){
    6.         return ref Ready;
    7.     }
    8.  
    9.  
    10. // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.  
    14.  
    15.         int Action = Got();
    16.  
    17.         Action++;
    18.  
    19.         Debug.Log(Action.ToString());
    20.         Debug.Log(Got().ToString());
    21.    
    22.     }

    it's return log

    0
    1


    i miss somethink's ?
     
  11. Inter-Illusion

    Inter-Illusion

    Joined:
    Jan 5, 2014
    Posts:
    598
    Last edited: Sep 16, 2018
    kilik128 and one_one like this.