need help on this script

Hi All,

I making a small app to find the length of the small objects and radius.
I just want to move my vernier calipers mover object. How will i write script for the moving of the object.
can i get any code or any idea to do that, then i will implement.

      using UnityEngine;
using System.Collections;

public class XDrag : MonoBehaviour {
 
public float sensitivity = 0.02f;
private Vector3 v3Prev;
 
void Update () {
if (Input.GetMouseButtonDown(0)) {
v3Prev.x = Input.mousePosition.x;
}
if (Input.GetMouseButton (0)) {
transform.position.x += (Input.mousePosition.x - v3Prev.x) * sensitivity;
v3Prev.x = Input.mousePosition.x;
}
}
}

After writing my script i am getting an error like this:

Assets/Scripts/XDrag.cs(14,15): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable

Can i get any help.

Thanks

Shankar

Try replacing transform.position.x += ..... with transform.localEulerAngles.x += .....

1 Answer

1

In C# you cannot modify just one value of the transform vector variable. The answer is in the error message : Consider storing the value in a temporary variable

Vector3 newPos = transform.position;
newPos.x = newPos.x + (Input.mousePosition.x - v3Prev.x) * sensitivity;
transform.position = newPos;

@alu just close and delete more questions to save me having to do it dude

Hi @Fattie and @alucardj thank for u answers. I am trying to delete that question as per the suggestion of me Fattie But i am getting We're sorry, but this question already has some answers that you cannot delete So i can't.

It's already been answered, so just click on the grey tick to accept it (if it fixed your problem). You could change the title to something more suitable like Cannot modify a value type return value of UnityEngine.Transform.position for future people searching the problem.

You need to check if you actually click on the calipers first. I would suggest a raycast on GetMouseButtonDown, if it hit the calipers, set a boolean to true, then in GetMouseButton check that boolean is true first, if so then do all the other stuff.