Transform.position of object not the same as shown on the inspector
I have an object called "Snake Head" that starts at position (0,1,0) and I want to track it's position in a script that is assigned to another object. I have this:
public GameObject object_to_follow;
...
Debug.Log("Pos1: " + GameObject.Find("Snake Head").transform.position);
Debug.Log("Pos2: " + object_to_follow.transform.position);
With the last 2 lines inside an Update(), the first log gives me the correct and real-time positions of the "Snake Head", but the second line always prints (0.0,1.0,0.0).
Why is the second line not printing the correct coordinates? What is the most correct way to do this?
Are you sure the provided gameobject in the inspector is the instance of the prefab, located in your scene, and not the prefab itself?
Answer by lucadome · Jul 28, 2018 at 10:30 PM
Are you sure there aren't any other objects named "Snake Head"? Try this solution:
GameObject objectToBeFollowed;
void Start(){
objectToBeFollowed = GameObject.Find("Snake Head")
}
Your answer
Follow this Question
Related Questions
Hey Guys How to make a script '**private Transform Waypoint;**', 0 Answers
How do you access the transform of a gameobject and put it in a variable? 3 Answers
Help with Obstacle. Please Help. 1 Answer
Vector3.Lerp not working properly, making the player bounce around 2 Answers
how to move the enemy in the current position of the player but only ones? 0 Answers