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 Ochreous · Apr 19, 2013 at 04:50 AM · c#teleport

C# Teleport Script Error

Hi everyone, I'm trying to make a script where the player is teleported +0.7 in the z axis each time the key T is pressed. But I get this error Cannot modify a value type return value of 'UnityEngine.Transform.positon'.Any idea how to fix this?

 using UnityEngine;
 using System.Collections;
 
     public class Teleport : MonoBehaviour
     {
 
     void Update(){
         if (Input.GetKey(KeyCode.T)){
                 transform.position.z += 0.7;
             }
         }
     }
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
0
Best Answer

Answer by iwaldrop · Apr 19, 2013 at 04:52 AM

You can't modify the axes directly. Instead, do this:

 Vector3 position = transform.position;
 position.z += 0.7f;
 transform.position = position;
Comment
Add comment · Show 8 · 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 iwaldrop · Apr 19, 2013 at 04:56 AM 0
Share

Because this is a bit of a pain to do every time you want to manipulate axis values, you could make methods that extend Transform to do the work for you:

 public static class TransformExtensions
 {
     public static void $$anonymous$$oveOnZ(this Transform t, float amount)
     {
         Vector3 p = t.position;
         p.z += amount;
         t.position = p;
     }
 }

Then to use that method you'd just call transform.$$anonymous$$oveOnZ(0.7f);

avatar image Ochreous · Apr 19, 2013 at 04:57 AM 0
Share

Thank you for that. I couldn't figure out why it wasn't working.

avatar image kmeboe · Apr 19, 2013 at 06:30 AM 1
Share

You can also use the built in Transform.Translate() method, which accomplishes this as well.

avatar image cowasockytommy · Apr 19, 2013 at 06:40 AM 1
Share

transform.Translate(0,0.7f,0);

??

avatar image Bunny83 · Apr 19, 2013 at 07:53 AM 1
Share

@DangerousBeans: This is necessary because .position / .localPosition / ... are properties and not variables. To change the value of a property you have to assign something to it. You just "read" it and change a member of the temporal copy which won't affect the value of the property.

Properties are just methods. When you do

 transform.position.x += 1;

It's just short for

 transform.position.x = transform.position.x + 1;

Since position is a property you only call get get-method here. So it actually looks like that:

 transform.get_Position().x = transform.get_Position().x + 1;

get_Position() will return a copy of the Vector3 and you set the x value of it, but only on the copy.

When you do this ins$$anonymous$$d:

 transform.position = transform.position + Vector3.forward;

you actually do:

 transform.set_Position(transform.get_Position() + Vector3.forward);

Which will work since you invoke the setter.

Show more comments

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

15 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

Related Questions

C# Instant Teleport Script 1 Answer

C# OnControllerColliderHit Detection Problems 0 Answers

C# OnMouseExit Kill/Destroy GUILayout Element 1 Answer

How to check if an object is colliding with another from another script ? 1 Answer

How to check if string array contains duplicates C# 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