Help witj chaning Java to C#

I have a java code but i want to make it into C#. Below is the java:

function Update () {
if (Input.GetMouseButton(0)) {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
}
}
}

I have tried outting it to C# but i don’t know why it wont work, can someone help please:

using UnityEngine;
using System.Collections;

public class Movebody : MonoBehaviour {

public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
public RaycastHit hit;

void Update()
{
if(Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, 10))
{
mousPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
}
}
}
}

Below are the code with some small changes.

  • Deleted your hit and ray global variables, they are ignored in your Update function since you declare two new local variables with the same name.
  • Deleted your local hit variable, since you don’t really use it.
  • Instead of creating a new Vector3 you can use your ray to place the object 10 units from the camera.
  • If you want to create a new Vector3 anyway you have to put new in front of Vector3, so new Vector3(..bla..);
using UnityEngine;
using System.Collections;

public class Movebody : MonoBehaviour {

void Update()
{
if(Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, 10))
{
hit.transform.position = ray.GetPoint(10);
}
}
}
}

use This

using UnityEngine;
using System.Collections;

public class testing : MonoBehaviour {

public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
public RaycastHit hit;
public Vector3	mouse_Pos;

void Update()
{
if(Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, 10))
{
mouse_Pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
hit.transform.position= Camera.main.ScreenToWorldPoint(mouse_Pos);
}
}
}
}

Hey this doesn’t seem to work? I’ve attached the script to the main camera and the camera is in orthographic view, but i am getting three errors still;

ArgumentException: You are not allowed to call INTERNAL_CALL_ScreenPointToRay when declaring a variable.
Move it to the line after without a variable declaration.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:291)
Movebody…ctor ()
UnityEditorInternal.InternalEditorUtility:InspectorWindowDrag(Object[ ], Boolean)
UnityEditor.DockArea:OnGUI()

get_main can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

ArgumentException: get_main can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
Movebody…ctor ()

Comments 6th Line try Again Please

No erros but when i click on the object nothing happens

ok Great , Please clear your description here then I can be able to help better .

Thanks, I want to be able to click an object in game view, and be able to move it and then drop it in a position on screen?

more detail Please

I have an object that i want to move along the x and z axis not the y, and i want to be able to click on that object and be able to more it anywhere on the screen and when i lift my finger off the click button i want that gameobject to stay where i have moved it.

Please Add this script with Camera try here cube is my Object

var moveCube:boolean;


	function OnGUI()
	{
		if(GUI.Button(Rect(0,50,100,100),"Test"))
		{
		moveCube=true;
		}
	}

	function Update()
	{
		if(moveCube)
		{
		
		Cube=gameObject.Find("Cube");
		
		Cube.transform.Translate(Vector3(1,0,1));
		
		moveCube=false;
		
		}
	}

Yes this moves the cube in the z,x axis by pres the test button?

Now Its Done or You wants something more ? :slight_smile:

Yeah thanks heaps mate!