Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

convert js to C#

Discussion in 'Scripting' started by Matt Sich, Aug 2, 2010.

  1. Matt Sich

    Matt Sich

    Joined:
    Jul 31, 2010
    Posts:
    32
    Hey guys,

    I'm not any good with C# but I'm learning...
    could you help me fix up this code? I tried to convert it to C# but I get errors about the lines I marked with ">>>"
    The error is: "Expression denotes a 'type', where a 'variable', 'value' or 'method group' was expected


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class spheremovec : MonoBehaviour {
    6. bool  grounded = true;
    7. float jumpnum = 10;
    8. float Rollspeed = 4;
    9. public float panning;
    10. void  Update (){
    11.  
    12. panning = Input.GetAxis("Pan");
    13.  
    14.  
    15.  
    16.        float horiz = Input.GetAxis("Horizontal");
    17.         float vert = Input.GetAxis("Vertical");
    18.         >>>rigidbody.AddForce(Vector3(horiz/Time.deltaTime/Rollspeed,0,vert/Time.deltaTime/Rollspeed));
    19.     if(grounded){
    20.    
    21.                 if (Input.GetButtonDown ("Jump")) {
    22.                     float jump = 5/Time.deltaTime;
    23.                     grounded = false;
    24.                     float jumpup = jumpnum*jump;
    25.                
    26.                     >>>rigidbody.AddForce(Vector3(0,jumpup,0));
    27.                 }
    28.  
    29. }
    30. }
    31.  
    32. void  OnCollisionEnter ( Collision theCollision  ){
    33.  
    34. grounded = true;
    35.  
    36. }
    37.  
    38.  
    39. }

    thank you very much!
    Matt
     
  2. Dusho

    Dusho

    Joined:
    Jul 10, 2010
    Posts:
    22
    shouldn't there be
    new Vector3(...)
    ?
     
  3. Matt Sich

    Matt Sich

    Joined:
    Jul 31, 2010
    Posts:
    32
    where?
     
  4. Dusho

    Dusho

    Joined:
    Jul 10, 2010
    Posts:
    22
    ...
    >>>rigidbody.AddForce(new Vector3(horiz/Time.deltaTime/Rollspeed,0,vert/Time.deltaTime/Rollspeed));
    ...
    >>>rigidbody.AddForce(new Vector3(0,jumpup,0));
    ...
     
  5. Matt Sich

    Matt Sich

    Joined:
    Jul 31, 2010
    Posts:
    32
    oh, ok. that worked. Can you explain why?
     
  6. Dusho

    Dusho

    Joined:
    Jul 10, 2010
    Posts:
    22
    C# syntax, variables need to be allocated
    you should maybe check JS <-> C# differences
     
  7. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Found here. It doesn't say anything, however, about having to use "new" to do allocation (you have to do allocation with JS, it's just obscured by the syntax and lets you delay specifying a type). In short, it's just the way things are done. The Javascript-flavored Unity scripting syntax tries to mimic Javascript syntax where everything is handled as though it's a function where C# hews closer to, well, C# and other statically-typed languages.

    If you read the "Writing scripts in C#" page and it's unclear that you need to use "new" to allocate objects in C#, consider filing a bug (using the in-Editor bug reporter) and mark the "Type of Problem" field as "Documentation." Give them the page URL and explain what's missing. That way, you'll help others avoid the same problem in the future. I've filed a few documentation bugs myself, and have found that UT is very receptive to suggestions on how to make the documentation better.
     
  8. Matt Sich

    Matt Sich

    Joined:
    Jul 31, 2010
    Posts:
    32
    thank you!

    looks like everyone is recommending C# over Unityscript. I'll start doing my best two learn it