Hi All,
i’ve read through the scripting overview a great number of times and am still having trouble truly understanding how to access member variables of components. In an attempt to actually understand it, i’ve created a scenario that if I can get it to work, I should then understand. Who knows maybe eventually i’ll actually be able to answer a question or two here on the forums…
var ObjectA : GameObject;
var ObjectB : GameObject;
var Midpoint : Transform.vector3;
var NewObject : GameObject;
function Update(){
//Get position of ObjectA
GetComponent (ObjectA.Transform);
Debug.Log (ObjectA.Transform);
//Get position of ObjectB
GetComponent (ObjectB.Transform);
//Calculate the midpoint position between ObjectA and ObjectB
//Midpoint = ????
// If up arrow is pressed, Instantiate a new object at the midpoint position calculated above
if (Input.GetButtonDown("Vertical")) {
var clone : GameObject;
clone = Instantiate(NewObject, transform.position, transform.rotation);
print ("added a new object between ObjectA and ObjectB");
}
}
// y'ought to use lowercase letters for variables
var objectA : GameObject;
var objectB : GameObject;
var midPoint : Transform.vector3;
var newObject : GameObject;
function Update()
{
//Get position of objectA
var objAPos = GetComponent("objectA").transform.position;
Debug.Log ("ObjectAPos: " + objAPos);
//Get position of objectB
var objBPos = GetComponent("objectB").transform.position;
Debug.Log ("ObjectBPos: " + objBPos);
//Calculate the midpoint position between ObjectA and ObjectB
midPoint = objBPos - objAPos;
// If up arrow is pressed, Instantiate a new object at the midpoint position calculated above
if (Input.GetButtonDown("Vertical")) {
var clone : GameObject;
clone = Instantiate(newObject, midPoint, Vector3(0,0,0));
print ("added a new object between ObjectA and ObjectB");
}
// y'ought to use lowercase letters for variables
var objectA : GameObject;
var objectB : GameObject;
var midPoint : Transform.vector3;
var newObject : GameObject;
function Update()
{
//Get position of objectA
var objAPos = objectA.transform.position;
Debug.Log ("ObjectAPos: " + objAPos);
//Get position of objectB
var objBPos = objectB.transform.position;
// alternativly = objectB.GetComponent(Transform).position
Debug.Log ("ObjectBPos: " + objBPos);
//Calculate the midpoint position between ObjectA and ObjectB
midPoint = objBPos - objAPos;
// If up arrow is pressed, Instantiate a new object at the midpoint position calculated above
if (Input.GetButtonDown("Vertical")) {
var clone : GameObject;
clone = Instantiate(newObject, midPoint, Vector3(0,0,0));
print ("added a new object between ObjectA and ObjectB");
}
Ok. I think i’m understanding it. Couple of questions:
Public variables are put outside of functions so we can assign components/properties or whatever to them, right?
Private variables (those inside the function statement) are calculated instead. Because they’re calculated by the function, you normally don’t expose them in the inspector, right?
Not really, all variables outside functions can be private (not shown in inspector, can be accessed oNLY by this script and not by other scripts) or public.
Variables declared in a function, for example inside an update() function, exist only while that function is executed, so, for example, a var declared inside an update function exists just for this frame/update cycle, and then it will be deferenced and you won’t be able to use them.
I can’t understand what you are saying here… all variables, private/protected/public can be used in THAT script (self), some can’t be used (private) or modified (protected) by other scripts; beeing editable in the editor is just a feature of public vars, but it doesn’t include anything.
Well, here we have 2 errors:
vector3 does not exist, Vector3 exists instead (note the caps on the V)
Transform.Position means that you are trying to access the position of a transform istance; Transform is a class that has, inside it, a variable named Position.
Transform.Vector3 (considering the correction from above) means that you are trying to access the Vector3 variable inside transform, but transform does not have a variable named vector3 inside It! thus the error.
Phillip,
Thanks a ton for the clarification about variable declarations. I think I actually am starting to learn some of this.
With regards to the Vector3 error. I don’t fully understand. A few questions:
the lower case v was a typo on my part:sweat_smile:. It actually was defined as
hmm. I don’t entirely follow.
var midPoint : Transform.Vector3
does the above not work because the there is no member variable named Vector3 of transform? If this is the case, how would I specify that “var midPoint” is a vector3 formatted position?
When declaring variables you would put the variable’s type after the colon. Generally* a type is just the name of a class. So you can declare:
var thingy : GameObject;
or
var thingy : Transform;
but this will cause an error
var thingy : Transform.position;
because ‘position’ isn’t a class name, so it’s not a type. ‘position’ does HAVE a type, and that’s Vector3, so if you want to declare something you can assign Transform.position to you have to use position’s type:
var thingy : Vector3;
The exception to this is that commonly used types have short names you can use to declare them too, like ‘int’ which is really the class Int32, for example. You can still declare something as Int32, ‘int’ is just a shortcut.
This used to cause me some confusion when I started with Unity also.
What clarified it for me was when I started thinking like this:
Each script FILE you create is basically a new CLASS with the FILENAME being the class name.
The GetComponent function merely creates a pointer to that class
If you have any programming background then THAT single fact should clear things up for you infinity!
Inside a file, all functions can access all variables declared outside any function.
So by pointing to the file, your other scripts have access to all the variables not declared inside a function and has access to all the functions in that file.
If you want to hide any of the functions or variables, just add the word “private” before you declare it:
i.e. private var : somevar : int;
Once I understood this I went from completely confused be being able to do some amazing things that even astounded me when I thought about how easy it was…