- Home /
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!
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.
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
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?
I hadn't noticed this line wasn't in a method; see below from @smnerat
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?
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;
}
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?
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.