- Home /
Why does this tutorial make a new object.
Can someone tell me why this Java script from this tutorial makes a new object:
var thePrefab : GameObject;
function Start () {
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
}
I am Just learning Java script. I already know a litle C++ so I might be treating some code like C++, so just holler and tell me how to do it in Java.
Here are the things I do not understand about this script:
- What exactly is the command Instantiate
- Why Does setting the variable instance to Instantiate, run the command Instantiate. I would think it would set the value of instance to the text of the Instantiate command. So I could say some thing like "print var instance and it would show on screen, Instantiate(thePrefab, transform.position, transform.rotation)".
- I tried running the script with out setting the variable instance to Instantiate and it still made a new object, why then set variable instance to the instantiate command? I used the code:
var thePrefab : GameObject;
function Start() {
Instantiate(thePrefab, transform.position, transform.rotation);
}
Just to clear up a common misconception - JavaScript (EC$$anonymous$$AScript) is NOT Java. It is unrelated to Java. Don't confuse what you're learning about JS with what you might want to learn about Java someday.
Answer by skovacs1 · Sep 15, 2010 at 09:56 PM
- Read the docs on Instantiate. It creates an instance of an object in the scene.
- I'm not sure what you mean by "the text of the Instantiate command".
instance
is a variableInstantiate
is a function call which returns a value - this is pretty standard programming regardless of the language. If you wanted to store the string "Instantiate(thePrefab, transform.position, transform.rotation)", you would escape it with quotes like I just did. Instantiate
returns the object. Assigninginstance
to the returned object is so that you have the instance of the object in the scene to act upon. You don't have to do anything with the returned value so you can easily call the function without assigning its return value to anything.
In number 3 are you saying that assigning var instance to instantiate makes it so it can refer to the new object and command it later by referring to it as an dinstance?
Sort of. Your wording is fairly confused. You don't make the var instance to Instantiate, but you assign the variable instance to refer to the returned value of Instantiate. Acting upon a variable will act upon the object it refers to, but the variable is not actually the object and this becomes meaningful in some situations. It's not really that you 'command it' in this context, but you operate upon and/or perform/call functions upon and/or execute commands upon the variable which may in turn act upon the instance it refers to.
Also, it's not that you are referring to it as an instance because it is an instance regardless of how you refer to it. Whether you assign a variable to store the instance or not, the instance will exist, and so it's not so much that you are referring to it 'as an instance', but that you are referring to the created instance in the scene through the variable instance. When you refer to the variable instance, for most purposes, you are referring to the instance to which it was assigned.
Classes are like a definition for the type of something and instances like actual objects of that type. Variables refer to these instances. Variables are like telephones, they are connected to someone or another and anything you say to the receiver will be said to the person or people they're connected to, but the phone isn't actually the people on the other end. Whether you connect to anyone or not, the people still exist - each person/group of people you could connect to would be like an instance. These concepts are fairly common to most object-oriented and imperative program$$anonymous$$g languages.