var target : Transform; what is that?

Doing the tutorial on scripting. can someone explain to me line by line the following script from page 4?

var target : Transform;
function Update () {
  transform.LookAt(target);
}

Thanks

That code defines a public variable named “target”, and the type of that variable is a transform (a position, rotation and scale in the scene). By making a variable public it’s exposed in the inspector (after attaching the script to an object in your scene, select that object and you’ll see the variable in the inspector). By exposing variables in the inspector you can set their values quite easily, either by directly typing them in or for something like a transform or GameObject you can drag assign a value (drag some other object from the scene to the variable slot in the inspector).

In this case a variable named target is defined so you can do all of the above and set it to be a reference to some in-scene object. Then the code in question will run every frame (Update is called each time the engine renders your scene), and when it does it points the current object so it “looks at” the “target” object you assigned.

Make sense? :slight_smile:

Note that the tutorial does explain all this, but you have to be reading it all in-line. For example, the pages leading up to that bit discussing exposing variables in the inspector. The section you’re quoting talks about “connecting variables” (the act of drag assignment which is a powerful workflow enhancement). Then the behavior of LookAt is at least roughly described at the top of page 5. :slight_smile:

Tip: when you post code on the forums, use the “Code” button up top, or manually wrap it in a code block ([ code ]…[ /code ], without the spaces) for better formatting. I’ll edit your post so you can see that as an example.

1 Like

Interesting what are the other TYPE of variable in unityScript or JS?

There are many and I don’t really want to list them all here. Suffice it to say you can use simple data types like int, float, boolean and String. Or you can use more involved data types like Transform, GameObject or even specific component types.

Keep working through the tutorial and don’t worry about all that just yet. Have fun!