• 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 dkfister · Oct 25, 2012 at 01:15 PM · transformpositionchildren

Change position directly

I have it like this, when i press a button. In this case A or D, my cube (Children of my parent) has to move to the opposite side of my Player (Holding the cube). So the cube is the way, the character is facing / moving. 2D Platformer.

This is my code, that should work, but it doesn't.

 transform.GetChild(0).transform.position.x = transform.position.x - 1.5f;

Im getting the error:

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

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
2
Best Answer

Answer by cfloutier · Oct 25, 2012 at 01:25 PM

You've got to add a temp var like this

 Vector3 pos = transform.GetChild(0).position;
         pos.x = pos.x - 1.5f;
         transform.GetChild(0).position = pos;

Because you can't change directly x, y z values of a transform position. but you can assign a new position.

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 dkfister · Oct 25, 2012 at 03:15 PM 0
Share

After doing some research, and trying out your solution. I found out that this would be alot easier and do my work.

transform.GetChild(0).transform.position = new Vector3(transform.position.x - 1.5f, transform.position.y, transform.position.z);

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to move a gameObject without affecting its childrens' positions? 2 Answers

Set Game Object Position to Sides of a Camera 1 Answer

Teleporting Player to a point using box colliders 1 Answer

How to place the GameObjects in a sequence on the plane 1 Answer

instant move an object to a position + offset value 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges