Search Unity

Difference between c# and JS

Discussion in 'Scripting' started by SpookyFishGames, Jan 28, 2013.

  1. SpookyFishGames

    SpookyFishGames

    Joined:
    Jan 28, 2013
    Posts:
    27
    I'm working through some tutorials to get the hang of how Unity works, and I'm having a problem. Most of the tutorials in the books/sites I've found use JS but I'd rather use c# as I am more comfortable with it. So as I follow the tutorials, I tend to convert the JS->C# on the fly as I work through them. This has been going fine, but I have run into one thing that works in JS but won't in C#.

    Javascript :
    Code (csharp):
    1.  
    2.     wheelT[0].localEulerAngles.y=horizontal*40;
    3.     wheelT[2].localEulerAngles.y=horizontal*40;
    4.  
    This works fine. But when I use the same code in C# I get the following error in Unity when it's compiled/interpreted :

    Code (csharp):
    1.  
    2.  error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localEulerAngles'. Consider storing the value in a temporary variable
    3.  
    Now I've tried a few things including creating temporary variables, as it recommends, But whatever I do, if I try and assign a value to localEulerAngles, or any of it's properties I get the same error. I guess I should be able to, because it works in JS, and the help file even states :

    I am setting it to an absolute value, so what's the problem?
     
  2. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    Only a small difference in C#:

    Code (csharp):
    1.  
    2. wheelT[0].localEulerAngles = new Vector3(wheelT[0].localEulerAngles.x, horizontal * 40, wheelT[0].localEulerAngles.z);
    3.  
    *Note, untested.
     
  3. SpookyFishGames

    SpookyFishGames

    Joined:
    Jan 28, 2013
    Posts:
    27
    Thanks for that, worked a treat :)