Converting Code from JavaScript to C# (570375)

I am rather new to scripting but I have been learning a lot. After watching a tutorial I have an awesome touch control script, but I am trying to convert it over from JavaScript to C#.

Here is what I have in JavaScript:

#pragma strict

public  var gravityDown : float;

private var ray : Ray;
private var hit : RaycastHit;

function Update ()
{
    if(Input.GetMouseButtonUp(0))
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray,hit))
        {
            transform.position.x = hit.point.x;
            transform.position.y = hit.point.y;
            this.GetComponent.<Rigidbody2D>().gravityScale = gravityDown;
        }
    }      
}

Here is my attempt at re-writing it in C#:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
    //gravity multiplier
    public float gravityDown;

    //used for raycasting
    public Ray ray;
    public RaycastHit hit;

     // Update is called once per frame
    void Update ()
    {
        if (Input.GetMouseButtonUp (0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if(Physics.Raycast(ray,hit))
            {
                transform.position.x = hit.point.x;
                transform.position.y = hit.point.y;
                this.rigidbody2D.gravityScale = gravityDown;
            }
        }
    }
}

I get the following errors:

Assets/Scripts/PlayerMovement.cs(21,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit)’ has some invalid arguments

Assets/Scripts/PlayerMovement.cs(23,43): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable

Assets/Scripts/PlayerMovement.cs(21,36): error CS1620: Argument #2' is missing out’ modifier

Assets/Scripts/PlayerMovement.cs(21,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit)’ has some invalid arguments

I have been pretty stuck on this, but I feel like it is very close. The JavaScript runs perfectly and all I am doing is trying to re-write it in C#. If someone could point me in the right direction that would be great!

Try this:

           if(Physics.Raycast(ray,out hit))  // Note the 'out' keyword here
           {
                // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
                transform.position = new Vector3(hit.point.x, hit.point.y);
               this.rigidbody2D.gravityScale = gravityDown;
           }

This is just off the top of my head, so it might not be 100 correct. Hope that helps.

A couple of things.
The lines numbers posted don’t match the lines in your source …
Assets/Scripts/PlayerMovement.cs(21,36) actually refers to line 19.

In line 18 : remove the Type designator Ray because the variable ray has already been declared.

In line 19 : Where is the variable hit assigned a value ??
check the docs Unity - Scripting API: Physics.Raycast

OK here is what I got and it returns one error:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
    //gravity multiplier
    public float gravityDown;

    //used for raycasting
    public Ray ray;
    public RaycastHit hit;

    void Update ()
    {
        if (Input.GetMouseButtonUp (0))
        {
            if(Physics.Raycast(transform.position,out hit))  // Note the 'out' keyword here
            {
                // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
               [B][I] transform.position = new Vector3(hit.point.x, hit.point.y,);[/I][/B]
                this.GetComponent<Rigidbody2D>().gravityScale = gravityDown;
            }
        }
    }
}

Error: Assets/Scripts/PlayerMovement.cs(20,89): error CS0839: An argument is missing

Sorry about the and , tried to bold and italicize the area but I guess you can’t do that.

The errors usually give a good indication of what’s wrong.
"Error: Assets/Scripts/PlayerMovement.cs(20,89): error CS0839: An argument is missing"

If you look on line 20, you’ll notice this:
new Vector3(hit.point.x, hit.point.y, );

You can clearly see it’s missing the 3rd argument. :slight_smile:

Edit: The 3rd argument is Z. Since you’re dealing with 2D and likely do not care to modify the z, you can simply pass in it’s own z:
new Vector3(hit.point.x, hit.point.y, transform.position.z);

Further to the comments by @Juice-Tin

your statement : new Vector3(hit.point.x, hit.point.y, );
If you remove the trailing comma the constructor will create the Vector3 with a default value of 0 for the z axis

Kerry,

I thought I had it as a 0, sorry. Even after adding a 0 into the “z” spot (or if I leave it blank and removing comma) I get the following errors:

Assets/Scripts/PlayerMovement.cs(17,36): error CS10502: The best overloaded method match for Unity Engine. Physics.Raycast(UnityEngine.Vector3)’ has some invalid arguments.

Assets/Scripts/PlayerMovement.cs(17,36): error CS1615: Argument ’ #2" does not require ’ out’ modifier. Consider removing ’ out’ modifier

So does that mean I don’t need that “out” modifier? When I remove the “out” modifier i get another error that states:

Assets/Scripts/PlayerMovement.cs(17,36): error CS1503: Argument ’ #2’ cannot convert ’ UnityEngine.RaycastHit’ expression type ’ UnityEngine.Vector3

This line appears to be giving me the most issue. Should I use another type other than “out”?

May we see the code after changes, please? Specific when you removed “out” (although i think is needed there).

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
    //gravity multiplier
    public float gravityDown;

    //used for raycasting
    public Ray ray;
    public RaycastHit hit;

    void Update ()
    {
        if (Input.GetMouseButtonUp (0))
        {
            if(Physics.Raycast(transform.position, hit))  // Note the 'out' keyword here
            {
                // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
                transform.position = new Vector3(hit.point.x, hit.point.y,0);
                this.GetComponent<Rigidbody2D>().gravityScale = gravityDown;
            }
        }
    }
}

Errors are as follows:

Assets/Scripts/PlayerMovement.cs(17,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3)’ has some invalid arguments

Assets/Scripts/PlayerMovement.cs(17,36): error CS1615: Argument #2' does not require out’ modifier. Consider removing `out’ modifier

I get the same errors if I remove the 0 from the “z” field.

@DarrikM7
The issue as I see it is as a result of your revisions — the Physics.Raycast arguments are now incorrect.

Essentially the Raycast should be told where to ‘fire’ from and in which direction ( the function has some default arguments that aren’t essential)

The original code used a Ray type variable which contains the to and from data.
The current code only uses the ‘from’ data ie the Vector3 transform.position.

These are the function signatures

public static bool Raycast(
Ray ray);

public static bool Raycast(
Ray ray,
float distance);

public static bool Raycast(
Ray ray,
out RaycastHit hitInfo);

public static bool Raycast(
Vector3 origin,
Vector3 direction);

public static bool Raycast(
Ray ray,
[DefaultValue("Mathf.Infinity")]  float distance,
[DefaultValue("DefaultRaycastLayers")]  int layerMask);

public static bool Raycast(
Ray ray,
out RaycastHit hitInfo,
float distance);

public static bool
Raycast(Vector3 origin,
Vector3 direction,
float distance);

public static bool Raycast(
Vector3 origin,
Vector3 direction,
out RaycastHit hitInfo);

public static bool Raycast(
Ray ray,
out RaycastHit hitInfo,
[DefaultValue("Mathf.Infinity")]  float distance,
[DefaultValue("DefaultRaycastLayers")] int layerMask);

public static bool Raycast(
Vector3 origin,
Vector3 direction,
[DefaultValue("Mathf.Infinity")]  float distance,
[DefaultValue("DefaultRaycastLayers")]  int layerMask);

public static bool Raycast(
Vector3 origin,
Vector3 direction,
out RaycastHit hitInfo,
float distance);

public static bool Raycast(
Vector3 origin,
Vector3 direction,
out RaycastHit hitInfo,
[DefaultValue("Mathf.Infinity")]  float distance,
[DefaultValue("DefaultRaycastLayers")]  int layerMask);

Regards, Kerry

afterthought:
IF the scene is 2D I’m wondering if the Physics.Raycast( … ) could/should be Physics.Raycast2D( … )

It is a 2D scene. When I make the the code:

transform.position=newVector2(hit.point.x, hit.point.y);

I get the following errors:

Assets/Scripts/PlayerMovement.cs(17,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3)’ has some invalid arguments

Assets/Scripts/PlayerMovement.cs(17,36): error CS1615: Argument #2' does not require out’ modifier. Consider removing `out’ modifier

It is still showing the error with a Vector3 which seems strange to me, and that ‘out’ modifier message is back again saying that I do not need it. It seems strange that the syntax for this part seems to be a lot different than the Javascript code (the Javascript is assuming a lot it seems). Here is what I have for the code at this point (just changed to a Vector2):

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
    //gravity multiplier
    public float gravityDown;

    //used for raycasting
    public Ray ray;
    public RaycastHit hit;

    void Update ()
    {
        if (Input.GetMouseButtonUp (0))
        {
            if(Physics.Raycast(transform.position,out hit))  // Note the 'out' keyword here
            {
                // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
                transform.position = new Vector2(hit.point.x, hit.point.y);
                this.GetComponent<Rigidbody2D>().gravityScale = gravityDown;
            }
        }
    }
}

Which direction is your Raycast firing ?

What is supposed to happen is I have that code attached to my player object, and when you click/tap the screen the object will jump to wherever you touched the screen (I am developing for Android). It works fine in the JavaScript version, just not in the C# version.

According to the documentation, the best call for your use is this one:
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers); Here:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
  //gravity multiplier
  public float gravityDown;

  //used for raycasting
  public Ray ray;
  public RaycastHit hit;

  void Update ()
  {
    if (Input.GetMouseButtonUp (0))
    {
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      if(Physics.Raycast(ray, out hit, 100)) // there is one more parameters (optional) that you specify the layer you firing the raycast
      {
        // Here you need to create a new Vector3 and assign that to transform.position. You can't modify the x and y directly.
        transform.position = new Vector2(hit.point.x, hit.point.y);
        this.GetComponent<Rigidbody2D>().gravityScale = gravityDown;
      }
    }
  }
}

just wondering: since it is a touch-screen, why not use Input.GetTouch? I haven’t use it yet, but in the documentation seems to have what you need. Have you tried?

Hmm I may try the Input.GetTouch and see what happens, I will also try the code you provided and see if I can get it working, thanks!