• 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
2
Question by BZS · May 24, 2014 at 08:20 PM · c#errortransform.positionreturn type

error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

Hi, I've been researching this error and how to fix it but I can't seem to figure out how. If you could point out what's wrong I would very much appreciate it.

 using UnityEngine;
 using System.Collections;
 
 public class CameraTrack : MonoBehaviour {
     public float xOffset;
     public float yOffset;
     public GameObject player;
     public Vector3 playerPos;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         playerPos = player.transform.position;
         transform.position.x = playerPos.x + xOffset;
         transform.position.y = playerPos.y + yOffset;
     }
 }

The error is at lines 17 and 18. Thanks in advanced!

Comment
Add comment · Show 2
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 BenLuker · Aug 21, 2016 at 08:11 AM 0
Share

I am getting the same error, even though I am using a temporary float. Can someone please help? What am I doing wrong?

 void StopBall ()
     {
         if (rigidbody.velocity.x <= stopVelocityThreshhold.x && rigidbody.velocity.x != 0f) {
             float tempX = 0;
 
             //Error occurs here
             rigidbody.velocity.x = tempX;
 
             Debug.Log("X Velocity Stopped");
         }
         if (rigidbody.velocity.z <= stopVelocityThreshhold.z && rigidbody.velocity.z != 0f) {
             float tempZ = 0;
 
             //Error occurs here
             rigidbody.velocity.z = tempZ;
 
             Debug.Log("Z Velocity Stopped");
         }
         if (rigidbody.velocity == Vector3.zero) {
             CancelInvoke();
         }
     }
avatar image Hellium BenLuker · Aug 21, 2016 at 08:15 AM 1
Share

You must not use a "temporary float", but a temporary "Vector3".

$$anonymous$$oreover, do not try to compare two vectors (with floating values) using the egality symbol == Compare the distance ins$$anonymous$$d.

  void StopBall ()
  {
      Vector3 velocity = rigidbody.velocity ;
      if (rigidbody.velocity.x <= stopVelocityThreshhold.x && rigidbody.velocity.x != 0f) {
          velocity.x = 0;
      }
      if (rigidbody.velocity.z <= stopVelocityThreshhold.z && rigidbody.velocity.z != 0f) {
          velocity.z = 0;
      }
      if ( Vector3.Distance( rigidbody.velocity, Vector3.zero ) < 0.01 ) {
          CancelInvoke();
      }
      rigidbody.velocity = velocity ;
  }

2 Replies

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

Answer by JasonBricco · May 24, 2014 at 08:31 PM

You can't set things to transform.position.x or transform.position.y. You need to do something like:

 Vector3 tempPos = transform.position;
 tempPos.x += xOffset;
 tempPos.y += yOffset;
 transform.position = tempPos;

PS: this isn't related to the question, but do consider caching the 'transform' on start, because I see you're using it in update every frame. It has a hidden GetComponent call and is a little performance intensive. So make a variable such as: Transform myTransform; and then at start say myTransform = transform;

Then you can refer to the transform as myTransform in update. Just a side note.

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

Answer by Jerryle · Mar 16, 2016 at 05:44 PM

SpriteState s= new SpriteState(); s.highlightedSprite = Instantiate(Resources.Load("image")); tf.GetComponent().spriteState = s;

Comment
Add comment · Show 1 · 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 Bunny83 · Mar 16, 2016 at 05:51 PM 1
Share

Uhm, this question:

  • is 2 years old

  • has an accepted answer with 4 upvotes which is correct

  • is about a very common gotcha and has been answered hundreds of time

In addition your answer:

  • doesn't explain anything,

  • contains unformatted code

  • has nothing to do with the actual question.

If you wanted to show that SpriteState has the same problem as it's also a value type, you could have posted a question about that and in addition post an answer that explains that case. Your answer just "necro'd" this question by posting a very poor answer that isn't related to the question...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

An NPC "moonwalks" 2 Answers

I can do this in JS or not ??? 2 Answers

NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnCollisionEnter2D (UnityEngine.Collision2D coll) 1 Answer

Adding value to GameObject[] 1 Answer

C# transform position 1 Answer


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