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 /
avatar image
0
Question by SHG · Jun 10, 2014 at 07:40 AM · transform.positionclampboundary

Object Boundaries C# help please

So, I'm sorry I'm not great with C# I'm just learning. I'm trying to make the mouse follow an object (which works), but I want some boundaries (so the object can't move off screen). I thought my code would work, but I'm getting error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable. I don't quite know why its not working, but I'll attach it and hope that somebody knows what to do.

 //the broken code
 using UnityEngine;
 using System.Collections;
 
 public class MouseMove2D : MonoBehaviour {
     
     private Vector3 mousePosition;
     public float moveSpeed = 1f;
     private float maxWidth = 16.6f;
     private float minWidth = -16.6f;
     private float maxHeight = 12.2f;
     private float minHeight = -12.2f;
 
     void Update (){
         mousePosition = Input.mousePosition;
         mousePosition =                Camera.main.ScreenToWorldPoint(mousePosition);
         transform.position = Vector3.Lerp( new  Vector3 (transform.position.x , transform.position.y, +1), mousePosition, moveSpeed);
         if (transform.position.x > maxWidth)
             transform.position.x = maxWidth;
         if (transform.position.x < minWidth)
             transform.position.x = minWidth;
         if (transform.position.x > maxHeight)
             transform.position.x = maxHeight;
         if (transform.position.x < minHeight)
             transform.position.x = minHeight;
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tarlius · Jun 10, 2014 at 08:15 AM

The clue is in the error. "Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable"

transform.position returns you a Vector3 which is a value type (in this case a struct). When you access a value type, a copy of it is made, so the version you get back is not the original, and any chances to it will not affect the original. This means that if you set the x on it, you set the x on the version of it that was returned to you, NOT the version in the transform. Ie:

 transform.position.x = 0;
 // is the same as:
 Vector3 pos = transform.position;
 pos.x = 0;

What you need to do is take transform.position, store it locally, edit the value, then set it again. For example:

 Vector3 pos = transform.position; // Returns you a *copy of* a the Vector3 position

 // Some logic to change pos
 pos.x = 0;

 transform.position = pos; // Update the original with the updated value.

Its a bit of a gotcha in c#. If you want to know more, look up the difference between value types (int, float, struct; get copied) and reference types (classes; get a reference to the original).

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Two methods are called on repet in my code and I don't know why 1 Answer

Camera Movement Bounds? 1 Answer

Help clamping player movement boundaries 1 Answer

Camera Boundry when changing orthographic size 0 Answers

How can I keep randomly moving enemies within a certain area (top-down 2D RPG)? 3 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