Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by BrandonW · Jul 23, 2016 at 04:25 AM · transformtransform.positiondrag-and-drop

Why can't I assign transform.position to a Vector3 object?

I am programming to make my objects have a 'drag-and-drop' behavior. When the mouse clicks on one, they drag, but the only difference is I want it to return to the original position when the user lets go of the mouse.

Here is my error:
//A field initializer cannot reference a non-static field, method, or property (UnityEngine.Component.transform) //The type UnityEngine.Vector3 does not take in a constructor that only has one argument

 Vector3 ogPosition = new Vector3(transform.position);  



In response to the errors; I thought transform.position is multiple arguments since it has x,y, and z values so I am not sure why it wouldn't work. I've searched for answers regarding this issue, but have not found any accurate sources.

Here is the rest of my code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class Drag : MonoBehaviour
 {
     Vector3 ogPosition = new Vector3(transform.position);
 
     float distance = 5.18f;
     void OnMouseDrag()
     {
 
         Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
         Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition); 
         //Takes in Vector Argument transforms screen view to world view
         transform.position = objPosition;
 
 
     }
     void OnMouseUp() 
     {
         Debug.Log("Drag ended!");
         transform.position = ogPosition;
     }
 
     void FixedUpdate()
     {
         
     }
 
 }


Thanks in advance!

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
2

Answer by Arshia001 · Jul 23, 2016 at 04:54 PM

This has nothing to do with Unity. C# does not allow field initializers besides constants, constructors and static fields, because to access a non-static field (position in this case) you'd need the constructed object, but field initializers run before the constructor. So you can have

 Vector3 myVector = new Vector3(0, 0, 0);

But you can't have:

 Vector3 myVector = new Vector3(transform.position.x, transform.position.y, transform.position.z);

That's the reason for your first error. Note that this only applies to fields (that is, variables declared at the class level) and not local variables declared in functions.

As for the second, Vector3 has no constructor which takes another Vector3 as input. You don't need it either, since Vector3 is a value type (read up on the differences between classes and structs in C#). What you can do to work around it, as @smnerat already showed, is to set the Vector3 at a later time, such as in Start().

Edit: If you have a hard time understanding my answer, it'll probably be a good idea to start with a few C# tutorials and move on to Unity only when you have a better understanding of the language.

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
1

Answer by getyour411 · Jul 23, 2016 at 04:27 AM

Vector3 ogPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);

You have to give the 3 arguments of tPos

Comment
Add comment · Show 3 · 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 BrandonW · Jul 23, 2016 at 04:34 AM 0
Share

Thank you for your response, but I have tried that and it still gave me the same error. i noticed when I place the assignment inside of a function like 'void OnStart' it works the way i had it originally.

Any clues why?

avatar image getyour411 BrandonW · Jul 23, 2016 at 04:45 AM 0
Share

I hadn't noticed this line wasn't in a method; see below from @smnerat

avatar image BrandonW getyour411 · Jul 23, 2016 at 06:41 AM 0
Share

Thank you and why can I only use transform.position inside of a function for assigning? Are functions the only thing that allow you to interact with the game object the script is attached to?

avatar image
1

Answer by smnerat · Jul 23, 2016 at 04:33 AM

Here's an edit since I didn't realize that it wasn't in a method either.

To use ogPosition in multiple methods, you need to declare it and then set it at a later time. You can also use @getyour411's answer as well inside of start()

 public Vector3 ogPosition;
 
 void start(){
      ogPosition = transform.position;
 }
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 BrandonW · Jul 23, 2016 at 06:40 AM 0
Share

Thank you and why can I only use transform.position inside of a function for assigning? Are functions the only thing that allow you to interact with the game object the script is attached to?

avatar image smnerat BrandonW · Jul 23, 2016 at 07:06 AM 1
Share

That's the gist of it. There may be a special case or exception to the rule just like anything else, but I can't think of one in this instance.

Unity actually has a ton of good videos on all kinds of beginner - intermediate subjects. Plus, in my experience, they're short and to the point, rather than some hour long tutorials that are basically someone prattling on for 50 $$anonymous$$utes peppered 10 $$anonymous$$utes of useful information.

I'm hesitant to answer your question because I'm having a hard time putting it into words, so take this with a grain of salt, because I'm not 100% sure that this is the technical reason it wasn't working.

I would say you were trying to initialize a variable with another variable. Basically, transform.position doesn't exist or mean anything until the script is actually being run, which is why the compiler was giving you an error.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject not following its Transform's Vector 1 Answer

My object is translating with speed but does not stop... 1 Answer

Is it possible to intercept variable alteration/function calls on an object (From within the object)? 0 Answers

Getting Transform position using mouse position 3 Answers

Getting the transform from a GameObject list 3 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