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
4
Question by chatoxz · Nov 25, 2011 at 01:16 AM · transformposition

transform.position

in c# the following code:

transform.position.y = 7.0f; give me this error:

Assets/Scripts/enemy.cs(19,35): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable.

A hand please, i don't know what's wrong.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
23

Answer by aldonaletto · Nov 25, 2011 at 01:27 AM

C# and Boo don't accept changes to single components of properties: you must copy to a variable, change the component and save it back:

 Vector3 temp = transform.position; // copy to an auxiliary variable...
 temp.y = 7.0f; // modify the component you want in the variable...
 transform.position = temp; // and save the modified value 
Comment
Add comment · Show 7 · 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 chatoxz · Nov 25, 2011 at 09:04 AM 0
Share

Thanks aldo, but after a while looking for an answer, i found a similar post. I'm just starting to use unity but it's looks like c# it's more complicated than javascript.

avatar image aldonaletto · Nov 25, 2011 at 09:51 AM 0
Share

Yes, C# is way more complicated than javascript - too complicated for a script language, in my opinion. I use javascript 99% of time: it takes care of many annoying things for us, avoiding lots of typing. But learning C# is useful, because some things can only be done in this language - like declaring DLL external functions, for instance.

avatar image BCizme1 · Aug 31, 2013 at 07:01 AM 0
Share

I am the exact opposite, I almost exclusively use C#, even though it is more difficult, it runs faster than javascript, and I noticed that it can handle arrays when C# can't, but you can always use lists in their place.

avatar image aldonaletto · Aug 31, 2013 at 06:01 PM 1
Share

I don't believe C# runs faster than JS: both compile to very similar IL code. Install ILSpy in your machine and take a look at the IL code generated. You can also decompile it to C#, even if it was originally written in JS: the decompiled code is basically the same. About the Array class: it's very inefficient, thus you're better off without it - the List class is a much better solution for dynamic arrays.

avatar image Kaemalux · Sep 17, 2013 at 07:47 AM 0
Share

Thanks aldo, i tried your solution and even if it so simple, it works perfectly. I congratulate for your answers, always so useful! Thank you!

Show more comments
avatar image
13

Answer by Stardog · Aug 04, 2012 at 07:38 PM

Or you can do:

  transform.position = new Vector3(transform.position.x, 7f, transform.position.z);
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 MAYALS18 · May 30, 2018 at 07:51 AM

To make your object rotate, you need to make a variable which shows that and then put it into a Vector3.

 using UnityEngine
 
 public class Example : MonoBehaiviour {
     private float axisY = 7.0f;
     void exampleClass () {
        transform.position = new Vector3 (transform.position.x, axisY, transform.position.z);
     }
 }

@chatoxz

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

10 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

Related Questions

What is the reasoning of multiplying by Time.deltaTime 1 Answer

does not work check if dead 1 Answer

Help around an approach to a "Virtual Motion Table" in Unity 0 Answers

Camera movement 1 Answer

GameObject position and localPosition not changing in hiearchy, only in script. 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