- Home /
Difference between gameObject.transform and just transform?
Hello there! I have started to learn c# over the last few weeks and while programming a question came into my mind. If I have a script attached to a gameobject, what's the difference at calling "gameObject.transform" and just "transform". For example: If I want to get the current position of my Object I would do something like this:
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 pos1 = gameObject.transform.position;
Debug.Log("Position1: " + pos1);
Vector3 pos2 = transform.position;
Debug.Log("Position2: " + pos2);
}
}
But when tested it gives out the exact same information. What is the difference? And for what do I need the "gameObject" when I can get the same information without it? Thanks for your help in advanced!
Answer by Prastiwar · Feb 08, 2018 at 06:04 PM
You don't need to write gameObject, because it acces to THIS script as GameObject, what is necessary. The only one difference is that:
gameObject.transform is like : this.GetComponent<.GameObject>().GetComponent<.Transform>();
transform is just: GetComponent<.Transform> (); and Unity knows you want to call transform for THIS object. I would say gameObject.transform is "this.transform" and transform is "this.transform" aswell but I think just transform is better to performance.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Colliding with multiple objects to destroy other gameObject 4 Answers
SetActive is not working 2 Answers