I want to simply drag and drop objects without rigidbody or physics. I found a old script of this written in C#. Its not working for me - I get a “NullReferenceException”
Can somebody fix this or convert it into Javascript? Here the code:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider))]
public class MovePoint2 : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
Screen.showCursor = false;
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
void OnMouseUp()
{
Screen.showCursor = true;
}
}
Here the Error in detail: UnityEngine.Camera.WorldToScreenPoint (Vector3 position) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:225)
MovePoint2.OnMouseDown () (at Assets/Scripts/General Scripts/MovePoint2.cs:13)
UnityEngine.SendMouseEvents:DoSendMouseEvents()