Okay, here's the way I see it. Defining an object under the class header doesn't create the object, rather it, like you said, creates a reference, or a nickname to the object. Like if I were to implement two rigidbodies in a script, it'd read like...
Now all this code will do is activate two rigidbody components on whatever I assign the script to. It doesn't do anything with them, just that they're in there, and can now be referenced and interacted with. Kinda useless, but good for illustration.
The way I take it as, the private marked objects under the class header do nothing other than inform the script that I'm GOING to have 2 Rigidbody component in there. They're private classes, only meant to be accessible to anything within the class they're grouped under, and that their names will be rb1 and rb2. It's in the Start method where I actually call up and assign the components, to be fired off as soon as the script executes (which in this case, would be at the start of the game).
This part's pretty easy to understand. It's basically C# for dipshits like me. Straightfoward, logical, and not at all complicated when you come to understand the syntax and reasoning behind it.
Now if you want to get complicated, this is my annotations for a loop within a loop I had to do to spawn enemy waves for the Space Shooter tutorial. I feel like I have about an 80% grasp of what it's doing exactly, though I'm still a bit iffy on some things.
Code:
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x),
spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
if (gameOver)
{
restartText.text = "Press 'R' To Restart";
restart = true;
break;
}
}
}
// The coroutine referenced in Start, named SpawnWaves.
// Yield return new WaitFoSeconds (startWait);
// Yield is a coroutine specific keyword (I believe), telling the following script
// to wait to execute until the conditions to the right are met. Here, it's pausing
// the script on the first start to give the player some breathing room before the game
// begins. Return tells it to stop execution until WaitForSeconds times down, as defined
// by the startWait integer opened to be defined in the editor.
// while (true)
// While is a keyword that starts a loop, the (True) is a conditional, which sets the
// condition for when the loop activates and deactivates. With the line above considered,
// once the startWait instance counts down to 0, the condition for the while loop becomes
// true, activating the script below.
// for (int i = 0; i < hazardCount; i++)
// The keyword "for" activates another loop within the coroutine, which runs FOR a set number of
// instructions as specified below.
// the rest of the code means interger i is currently 0, if i is less than the hazardCount instance
// (defined from within Unity), then add another (i++ means current interger count +1) until it
// reaches the number set in hazardCount.
// Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
// Sets up Vector3 instance named spawnPosition, which uses a random range generator to
// first define a wide, random area where to spawn the hazards within the game space on x
// (no farther left than the negative value, no farther right than the positive), and where
// to spawn them on positions y and z.
// Quaternion spawnRotation = Quaternion.identity;
// This line of code sets up random rotations for our hazards. A quaternion is a measurement
// for rotation on 4 axes, and the name for this quat instance is spawnRotation.
// Quaternion.identify locks the quat rotation to itself on a local axis, preventing an object
// from spinning out of your playing field. Here, it keeps the hazards tumbling about, but still
// moving downwards towards the player on the Z axis.
// Instantiate (hazard, spawnPosition, spawnRotation);
// Instantiate makes an instance of an object. Here, it creates an instance of hazard, and applies
// the position and rotation instances set in the lines above to the object. Or..
// Make (this object, randomly placed within these constraints, tumbling like this)
// yield return new WaitForSeconds (spawnWait);
// About the same as the code above. Basically "hold up, Wait for the number of seconds
// specified in the spawnWait (defined in Unity), then rerun the For loop.
// if (gameOver)
// {
// restartText.text = "Press 'R' To Restart";
// restart = true;
// break;
// }
// pretty self explanatory. if (conditions for gameOver are met)
// then display text string "Press R to restart"
// and the conditions for a restart are met (hitting R)
// then end the entire loop.
// Restarting begins it all again, rerunning the loop from scratch.