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 EmreB24 · Jan 23, 2015 at 07:30 PM · c#transformpositionvector3

How to store position in a variable and than edit only specific part of it(such as 'x')

So I am trying to store a whole position in a variable and than trying to edit only a specific part of it such as 'x' or 'y'. I am pretty new on Unity so im not that much familiar with its API so please excuse me if i say something weird or even if my script is completely wrong.

So here is how i am trying to solve my problem:

 public Vector3 position = transform.position;
 position.x = 1.1f;

this is what i get as an error:

 Assets/Scripts/Player/Player.cs(10,27): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'

this is what i have at line 10:

 public Vector3 position = transform.position;
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
1

Answer by tanoshimi · Jan 23, 2015 at 07:38 PM

Well, you have two problems (one on each line :) You can't initialise a variable with a nonstatic field, and you can't edit a single member of a struct.

Try this:

 // First error is that you can't initialise the variable to transform.position here
 public Vector3 position;

 void Start() {

   // To prevent second (as yet unnoticed error), don't just assign the x member - 
   // Create a whole new struct instead.
   position = new Vector3(1.1f, transform.position.y, transform.position.z);
 }
Comment
Add comment · Show 2 · 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 EmreB24 · Jan 23, 2015 at 07:41 PM 0
Share

Thanks for the help!

so when i am going to call a specific location i am going to use poisiton.y or position.x, right?

avatar image EmreB24 · Jan 23, 2015 at 08:09 PM 0
Share

Oh and also, I tried to set my z coordinate to 0 zero if my TwoDimention boolean is true, but it didnt worked. No errors on my console.

 if (TwoDimention == true)
         {
             position = new Vector3(transform.position.x, transform.position.y, 0);
         }


if statement is inside of a Update void

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

2 People are following this question.

avatar image avatar image

Related Questions

Trouble setting up a vector 3. 1 Answer

Script to make Camera follow the player C# 3 Answers

Issue's With Positioning 0 Answers

Guided missile help c# 1 Answer

How to rotate an object to face the direction it's going? 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