Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 29, 2013 at 06:57 PM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by Shankar · Apr 29, 2013 at 04:02 PM · physicsmousemousedragobjects-movement

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ExTheSea · Apr 29, 2013 at 05:34 PM 0
Share

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

1 Reply

  • Sort: 
avatar image
2

Answer by AlucardJay · Apr 29, 2013 at 06:09 PM

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;
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Apr 29, 2013 at 06:58 PM 0
Share

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

avatar image Shankar · Apr 30, 2013 at 04:55 AM 0
Share

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.

avatar image AlucardJay · Apr 30, 2013 at 05:01 AM 0
Share

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.

avatar image Shankar · Apr 30, 2013 at 05:15 AM 0
Share

Thanx @alucardj. The vernier scale is moving in x-axis but the problem is that it should only move when I drag the scale, but its also moving on the empty screen background if i drag my mouse in empty screen.

 using UnityEngine;
 using System.Collections;

 public class XDrag : $$anonymous$$onoBehaviour {
  
 public float sensitivity = 0.02f;
 private Vector3 v3Prev;
  
 void Update () {
 if (Input.Get$$anonymous$$ouseButtonDown(0)) {
 v3Prev.x = Input.mousePosition.x;
 }
 if (Input.Get$$anonymous$$ouseButton (0)) {
             Vector3 newPos = transform.position;
 newPos.x = newPos.x + (Input.mousePosition.x - v3Prev.x) * sensitivity;
 transform.position = newPos;
 v3Prev.x = Input.mousePosition.x;
 }
 }
 }

I have used this script for the vernier scale.

can please help me on that.

avatar image AlucardJay · May 01, 2013 at 08:23 AM 0
Share

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

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to realize the real world physics? 1 Answer

Making an object fly at selected item. 0 Answers

2.5D Side-scroller Mouse Drag Objects 1 Answer

Having more than one collider in a GameObject? 8 Answers

How to create normal physics reactions when collision is caused by a GameObject controlled by mouse position 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges