• 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
3
Question by CoastersPaul · Apr 18, 2015 at 08:45 PM · transformvector3transform.position

How do I change one value of a vector?

If I'm not mistaken, I've used code like this before:

transform.position.z = [value]; //but now it doesn't work.

Instead, it gives the following error: error CS1612: Cannot modify a value type return value of 'UnityEngine.Transform.position'. Consider storing the value in a temporary variable.

transform.position[3] = [value]; //doesn't work either.

I thought I found a way to get it to work, but it's incredibly longwinded, redundant, and ugly:

transform.position.Set(transform.position.x, transform.position.y, [value]); /*Gross, and edit: still doesn't work.*/

Is not being able to do the earlier ones a bug? Is there any better workaround?

(Making a temporary variable isn't fun - my code's now 21 lines instead of 9.)

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

2 Replies

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

Answer by WillNode · Apr 18, 2015 at 09:01 PM

you need to make a temporary variable first.

 Vector3 position = transform.position;
 position[2] = [value]; // the Z value
 transform.position = position;
Comment
Add comment · Show 4 · 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 CoastersPaul · Apr 18, 2015 at 09:12 PM 0
Share

Well, that's what the error said, too, but I could have sworn there was a way that didn't take up three lines. Am I just remembering things wrong?

avatar image WillNode · Apr 18, 2015 at 09:15 PM 0
Share

well, if you write this on JS it should work.

in C# you need to make temporary variable first.

avatar image screenname_taken · Apr 18, 2015 at 09:34 PM 1
Share

In Unity script you don't have to declare a variable. You just go transform.position.x= 1.234;

avatar image Lo0NuhtiK · Apr 18, 2015 at 10:32 PM 1
Share

C# one-line :

 transform.position = new Vector3(transform.position.x, transform.position.y, zValue) ;
avatar image
6

Answer by ripperdave · Aug 19, 2017 at 09:26 AM

Basically it is the same as the ideas before. You can create a static class in c#. You can use shorter class and method names as well. ChangeX to X etc.

 public static class Utils {
 
 
     public static Vector3 ChangeX(Vector3 v, float x)
     {
         return new Vector3(x, v.y, v.z);
     }
 
     public static Vector3 ChangeY(Vector3 v, float y)
     {
         return new Vector3(v.x, y, v.z);
     }
 
     public static Vector3 ChangeZ(Vector3 v, float z)
     {
         return new Vector3(v.x, v.y, z);
     }
 
 }

Usage in a different class.

 public class Something: MonoBehaviour
 {
  void Update() {
      transform.position = Utils.ChangeX(transform.position, 1); // change x
 }
 }



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 kubpica · Aug 18, 2018 at 12:08 AM 0
Share

It can be shortened to:

 transform.position = transform.position.X(1); //change x

Here's the class I use:

 using UnityEngine;
 
 /// <summary>
 /// An auxiliary class containing methods that help change only one of the structure values.
 /// </summary>
 /// <example>
 /// Usage:
 /// <code>
 /// gameObject.transform.localPosition = Change.Y(gameObject.transform.localPosition, f*0.10f);
 /// </code>
 /// or
 /// <code>
 /// gameObject.transform.localPosition = gameObject.transform.localPosition.Y(f*0.10f);
 /// </code>
 /// </example>
 public static class Change {
 
     public static Vector2 X(this Vector2 v, float x)
     {
         v.x = x;
         return v;
     }
 
     public static Vector2 Y(this Vector2 v, float y)
     {
         v.y = y;
         return v;
     }
 
     public static Vector3 X(this Vector3 v, float x)
     {
         v.x = x;
         return v;
     }
 
     public static Vector3 Y(this Vector3 v, float y)
     {
         v.y = y;
         return v;
     }
 
     public static Vector3 Z(this Vector3 v, float z)
     {
         v.z = z;
         return v;
     }
 
     public static Color R(this Color c, float r)
     {
         c.r = r;
         return c;
     }
 
     public static Color G(this Color c, float g)
     {
         c.g = g;
         return c;
     }
 
     public static Color B(this Color c, float b)
     {
         c.b = b;
         return c;
     }
 
     public static Color A(this Color c, float a)
     {
         c.a = a;
         return c;
     }
 }

Usage:

 gameObject.transform.localPosition = Change.Y(texture.transform.localPosition, f*0.10f);

or:

 gameObject.transform.localPosition = texture.transform.localPosition.Y(f*0.10f);

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

6 People are following this question.

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

Related Questions

How to get a proper ramming effect? 1 Answer

MOVE GameObject to point A to B and to B to A 2 Answers

collider triggers transform position 1 Answer

Trouble converting transform.position to C# 1 Answer

fixed distance for Vector3.moveTowards ? 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