Home > Software design >  Drag and Drop C# (Unity 2D)
Drag and Drop C# (Unity 2D)

Time:08-25

So I am a newbie Unity dev and am making a game that involves a drag and drop system, I watched a tutorial and got help from a friend but it won't work... an error I get is it says public isn't valid or what ever, same for private.

public class Draggable : MonoBehaviour
{
Vector2 difference = Vector2.zero;
}
private void onm ouseDown()
{
difference = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
}
private void onm ouseDrag()
{
transform.position = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - difference;
}

here is a picture of the error

CodePudding user response:

Your functions are written outside the class. Correct the parentheses.

public class Draggable : MonoBehaviour
{
    Vector2 difference = Vector2.zero;
    private void onm ouseDown()
    {
        difference = (Vector2) Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    }

    private void onm ouseDrag()
    {
        transform.position = (Vector2) Camera.main.ScreenToWorldPoint(Input.mousePosition) - difference;
    }
}

CodePudding user response:

Your Methods are outside the scope of the class, ensure of move then into the body of your class inside the block "{ }"

  • Related