Trying to drag a GameObject with a Sprite renderer and 2d Collision Box. I try to run this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pieceMovement : MonoBehaviour
{
private float startPosX;
private float startPosY;
private bool beingHeld = false;
// Update is called once per frame
void Update()
{
if(beingHeld)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x, mousePos.y, 0);
}
}
private void OnMouseDown()
{
if(Input.GetMouseButtonDown(0))
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
beingHeld = true;
}
}
private void OnMouseUp()
{
beingHeld = false;
}
}
But I get this error:
NullReferenceException: Object reference not set to an instance of an object
pieceMovement.OnMouseDown () (at Assets/pieceMovement.cs:37)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)