Instantiation of Boundary Class in Space Shooter tutorial?

Hello everyone! I’m new to Unity, though I’ve been developing web applications for a while already. I finished the 2D tutorials as a start, but I’m now going through the Space Shooter one to get my feet wet with the engine.

In this particular tutorial:
http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player
Specifically, at about the 15 minute mark, there’s a class called Boundary used to stop the player’s ship from moving out of the camera.

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;

}

public class PlayerController : MonoBehaviour {
    public float speed, tilt;
    public Boundary boundary;

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal"), 
            moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
        rigidbody.velocity = movement * speed;
        rigidbody.position = new Vector3(
            Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)
        );

        rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
}

My question is: When is the Boundary object instantiated? I can see it being declared in line 10 above, but I see no initialization anywhere at all, however, it can be used just fine in lines 19 to 21 without causing an exception or breaking anything. What sorcery is Unity pulling off behind the scenes? And how/when can I consider that Unity will do the same for other custom classes I eventually make?

If I can read about it in the Unity documentation, feel free to point it out to me. I’ve been neglecting it because it’s kinda huge so I’m more comfortable with videos :stuck_out_tongue:

It is not instantiated by code. the playercontroller class is a monobehavior and thus probably attached to a gameobject. the boundary is a public variable of that playerctrl class. when you click on the gameobject with that class you can see it. so it is “magically” created by unity. this is required that unity can provide you the values in the inspector as it often does not run your initialization code in edit mode.
note the [System.Serializable] above the boundary class which ensures unity can serialize the values you set there in the inspector.
also note that inspector values “overwrite” values you set in code for public variables. this way you can provide the standard values by code and create variations of it by tweaking the values in the inspector window.

when you have a public variable of a serializable class or a private one marked with serialize field.

Thank you, that makes sense.

For simplicity’s sake, I understand that as long as Unity can see the variables in the Inspector they can be initialized outside of the code. A runtime exception should occur if a member is used, but isn’t initialized neither in the Unity editor nor in the code.

yes, thats the infamous null reference exception. all stuff you can’t see in the inspector is up to your responsibility to create objects for.