S
S
SkrinV2019-09-22 09:38:37
C++ / C#
SkrinV, 2019-09-22 09:38:37

How to change the position of an object with the mouse?

There is a script that, according to the idea, should move the object (as well as increase it) when the mouse is pressed on the object itself
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class scale : MonoBehaviour
{
Vector3 pos = new Vector3();
private void OnMouseEnter()
{
transform.localScale = new Vector3(0.30f, 0.02f, 0.50f);
}
private void OnMouseExit()
{
transform.localScale = new Vector3(0.25f, 0.01f, 0.45f);
}
void OnMouseDrag()
{
transform.position = Vector3(pos);
}
}
But the unit gives an error
Assets\Script\scale.cs(20,30): error CS1955: Non-invocable member 'Vector3' cannot be used like a method.
How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rikonardo, 2019-09-22
@SkrinV

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class scale : MonoBehaviour
{
Vector3 pos = new Vector3();

private void OnMouseEnter()
{
transform.localScale = new Vector3(0.30f, 0.02f, 0.50f);
}

private void OnMouseExit()
{
transform.localScale = new Vector3(0.25f, 0.01f, 0.45f);
}
void OnMouseDrag()
{
transform.position = new Vector3(pos);
}
}

In theory it should work. The problem is that you didn't call the constructor in the OnMouseDrag() function (you didn't specify "new")
UPD : I reviewed your code and realized that what I wrote most likely will not work. You don't need a Vector3 constructor at all, you just need to specify
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class scale : MonoBehaviour
{
Vector3 pos = new Vector3();

private void OnMouseEnter()
{
transform.localScale = new Vector3(0.30f, 0.02f, 0.50f);
}

private void OnMouseExit()
{
transform.localScale = new Vector3(0.25f, 0.01f, 0.45f);
}
void OnMouseDrag()
{
transform.position = pos;
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question