Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Setting a variable as public Transform ?

Discussion in 'Scripting' started by MitkoBachvarov, Dec 4, 2015.

  1. MitkoBachvarov

    MitkoBachvarov

    Joined:
    Nov 22, 2015
    Posts:
    5
    Hello, I am new to Unity3D and am currently going through the basic scripting tutorials, when I saw that in tutorial 14. Look At, in the code, the variable "target" is set to public Transform target.
    Now my question is: When and why should we set a variable to be of type Transoform? And what does that actually mean?
    Thanks!
     
  2. MaxGaniyev

    MaxGaniyev

    Joined:
    Mar 18, 2010
    Posts:
    29
    Hi Mitko,

    (Firstly sorry for bad English)
    I can't say I'm a professional, but however I know Transform is the component of GameObject that responsible for position, rotation and scale of that GameObject. If I right in the tutorial GameObject is looking at another GameObject? then this public Transform target is Transform component of second GameObject, first GameObject is looking at position of target GameObject, so we are getting position parameter through Transform. If variable marked as "public" then You can edit this variable directly from editor, so you can drag and drop your target GameObject's Transform component to your target variable in editor.

    Max.
     
    MitkoBachvarov likes this.
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Transform is a Component of an GameObject. It stores several informations about position, rotation for example.
    It also has some function you will need to use.

    Detailed inforamtions about which variables and function it hast you can read here:
    http://docs.unity3d.com/ScriptReference/Transform.html

    so you can reference a gameobjects transform to a variable Transform myTransform.

    normally you use in scrips just transform.position because every Component has already internal somehow cached the Transform Component of his GameObject.

    but if you need another transfrom of another Gameobject you can make a public Transfrom myTransform variable, than drag a gameobject into this field in inspector so you can than use this in your script.

    Code (CSharp):
    1. public Transform myTransform; //drag an gameobject into this field in inspector
    2.  
    3. // in methods use
    4.  
    5. myTransform.position
    6.  
    you could also make a public GameObject myGameObject

    Code (CSharp):
    1. public GameObject myGameObject; //drag an gameobject into this field in inspector
    2.  
    3. // in methods use
    4.  
    5. myGameObject.transform.position
    6.  
    but in this case you have to use myGameObject.transform.position to change positions etc.
     
    MitkoBachvarov likes this.
  4. himanshugupta159

    himanshugupta159

    Joined:
    Jul 14, 2018
    Posts:
    2