- Home /
Accessing Vector3 variable from another script and having the object move towards that location
Hi, in ScriptA I have
animal.GetComponent<Patrol>().**target2** = new Vector3(transform.position.x, 3.580481f, transform.position.z);
The animal Object that contains ScriptB calls this
transform.position = Vector3.MoveTowards(transform.position, **target2**.transform.position, Time.deltaTime);
I get the following error: Type 'UnityEngine.Vector3' does not contain a definition for 'transform' and no extension method transform of type 'UnityEngine.Vetor3' could be found. . .
What am I doing wrong??
Answer by AlucardJay · Sep 06, 2014 at 10:24 PM
target2 is a Vector3, not a transform. you are trying to access the position variable of a Vector3 which doesn't exist. A Vector3 only has the variables outlined in the last link posted below.
A transforms position variable is a Vector3. You already have a Vector3 (target2) so just use that :
transform.position = Vector3.MoveTowards(transform.position, target2, Time.deltaTime);
http://docs.unity3d.com/ScriptReference/Transform.html
http://docs.unity3d.com/ScriptReference/Transform-position.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613162446im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I want to move a cube with rotation but I find this problem 1 Answer
transform.position error 1 Answer
Why is my variable updating when it shouldn't? 1 Answer
How to move a gameObject without affecting its childrens' positions? 2 Answers
How to assign x and y transform values of an gameobject to user defined variables? 1 Answer