Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Info help Trandform vs GameObject

Discussion in 'Scripting' started by Metalbreath, Mar 23, 2013.

  1. Metalbreath

    Metalbreath

    Joined:
    Jul 27, 2012
    Posts:
    90
    Hello everyone.
    I have a question. what is the difference between

    Code (csharp):
    1.  
    2. void Start ()
    3.     {
    4.         nextPosition = transform.localPosition;
    5.         for (int i =0; i < numberOfObjects; i++)
    6.         {
    7.             Transform o  = Instantiate (prefab) as Transform;
    8.             o.localPosition = nextPosition;
    9.             nextPosition.x += o.localScale.x;
    10.         }
    11.     }
    12.  
    13.  
    and

    Code (csharp):
    1.  
    2. void Start ()
    3.     {
    4.         nextPosition = transform.localPosition;
    5.         for (int i =0; i < numberOfObjects; i++)
    6.         {
    7.             GameObject o  = Instantiate (prefab) as GameObject;
    8.             o.localPosition = nextPosition;
    9.             nextPosition.x += o.localScale.x;
    10.         }
    11.     }
    12.  
    13.  
    I cant understand when you HAVE to use GameObject and when to use Transform.
    Could anyone explain it to me so it will make more sense?

    Thanks in advance :)
     
    Last edited: Mar 23, 2013
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    First off: When pasting code to the boards, please make sure to put it into [code]... code here ... [/code] tags. That way your code will stay readable.

    Basically you need to differentiate between two aspects: A GameObject, and Components. A GameObject is the very basic class of what you put into your scene. Everything you can select in the hierarchy actually is a GameObject. So that is more or less the "carrier" of anything you want to do with that object.

    To any GameObject you might add different components. These components can be of various types. It can be a rigidbody, it can be a collider, it can be a MeshFilter and MeshRenderer to actually make your object look like something. Another component is the "Transform" component, which is a very basic one as well.

    In general, every GameObject also has a Transfom component attached. There's no way around that. It is the very basic component, and in general it serves as a piece you use any time you want to move, rotate or scale your GameObject. All that is done in the Transform component.

    Note: You can NOT move a GameObject directly without using the Transform component of it. That means that your second code is actually not going to work since the GameObject does not have properties for positions.
     
  3. Metalbreath

    Metalbreath

    Joined:
    Jul 27, 2012
    Posts:
    90
    let me see if i understood correctly.

    we can use a "var object GameObject" when we want to work on the Object in general.
    and we use "var object Transform" when we gona need the function of the Object to be transformed

    P.S. Thanks for the note
    Code (csharp):
    1. . i edited the script :)
     
  4. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669