- Home /
Question about Unity_Scripting_Tutorial.pdf
In this unity tutorial http://www.ntua.gr/best/ac12/Unity_Scripting_Tutorial.pdf
it has this code
var newObject : Transform;
function Update () {
if (Input.GetButtonDown("Fire1")) {
Instantiate(newObject, transform.position, transform.rotation);
}
}
The part what I dont understand is the variable and where it says "transform.position transform.rotation." what does the transform mean after the variable and wouldn't you have to put newObject.transform.position?
Answer by aldonaletto · Aug 18, 2013 at 08:23 PM
The instruction:
var newObject: Transform;
defines a public variable called newObject whose type is Transform. In Unity, this variable appears in the Inspector as a field named New Object (the Inspector creates a label based on the variable name), and you must assign to it an object from the Project view (a prefab). The instruction Instantiate creates a clone of this object at/with the position/rotation specified. In this case, transform.position is the position of the object that has this script, and transform.rotation is its rotation. In other words, when the button "Fire1" is pressed, a clone of the object defined in newObject is created at the same position and with the same rotation of the owner of this script.
Answer by Nexonity · Aug 18, 2013 at 08:08 PM
transform.position and transform.rotation means the current position and rotation of the gameobject that the script is attached to.
If you have multiple game objects attached how do you specify which one?
I think you are mixing var newObject : Transform; with transform.position. The newObject : Tranform has nothing to do with transform.position. For example if you create a new variable like this: "var blah : Transform;" and then you change "tranform.position" with "blah.position" It will spawn at the position where the gameobject you assigned to "var blah : Transform"
var newObject : Transform;
var anotherObject : Transform;
var blah : Transform;
function Update () {
if (Input.GetButtonDown("Fire1")) {
Instantiate(newObject, blah.position, blah.rotation);
Instantiate(anotherObject, blah.position, blah.rotation);
}
}
I don't think I understand.. so, like this?
var Ob : Transform;
var obtwo : Transform;
function Update () {
if (Input.GetButtonDown("Fire1")) {
Instantiate(Ob, Ob.position, Ob.rotation)
Instantiate(obtwo, obtwo.position, obtwo.rotation)
}
}
Your answer
Follow this Question
Related Questions
Character Movement component 2 Answers
How to make a pirate ship game? 1 Answer
UI color issue 1 Answer
Is Unity Basic Allow to publish application on App Store? 1 Answer