MonoBehaviour Template for Beginners

Hello again!  Earlier this week I presented what I called a Pre-Global Game Jam training session for users new to Unity.  We recorded the session and despite my utter distaste for seeing myself on video, I thought it would be awesome to share it here… Unfortunately, I wasn’t quite prepared well enough to give the talk to over 70 attendees, so it ended up being a bit of a disaster :( .  Not all is lost though!  One of the things that is most useful is my “MonoBehaviour Template” which I created to help first-time Unity programmers deal with some difficult Unity issues.  I’m going to share that with you here.

To use this template, just copy + paste it into every new MonoBehaviour you write, and the comments will guide you through how to deal with certain issues.  You can download the template here, or you can copy it from below:

// You can uncomment either of these enable their functionality
//#define USE_SOLID_COLLISION
//#define USE_TRIGGER_COLLISION
 
using UnityEngine;
using System.Collections.Generic;
 
/**
 * TODO: Comment what this class does.
 * Note:  The name of your class must match your file name
 */
public class MonoBehaviourTemplate : MonoBehaviour
{
    // An example of a string that is editable by designers in the scene
    //public string                _myString;
     
    // An example of an integer that is NOT editable by designers in the scene (changed only through code).
    //protected int                _myInt;
     
    // An example of referencing multiple other gameObjects.  It is editable by designers.
    //public List<GameObject>        _otherGameObjects;
     
    /**
     * Awake is called ONCE whenever an object first becomes enabled.  This call preceeds the OnEnable call.
     * Use this call for single-use initialization that will remain throughout the lifetime of an Object.
     */
    void Awake()
    {
    }
     
    /**
     * OnEnable is called whenever BOTH this Component and the GameObject on which it resides are enabled.
     * This is true by default, so by default this will run as soon as the scene starts up or a prefab is instantiated.
     */
    void OnEnable()
    {
    }
     
    /**
     * OnDisable is called whenever EITHER this Component or its GameObject are disabled.
     * It may not be obvious, but this happens whenever a GameObject gets destroyed.
     * In this function, you should "undo" anything you did in OnEnable.
     */
    void OnDisable()
    {
    }
     
    /**
     * Update gets called every frame. If you need to keep track of the time passed, use Time.deltaTime.
     */
    void Update()
    {
    }
     
    /**
     * LateUpdate gets called every frame after all physics and animations have been computed.
     * Typically, you put Camera movement in here as you want it to be computed last.
     */
    void LateUpdate()
    {
    }
     
    /**
     * FixedUpdate is called every "Physics-Tick".  This means it can be called multiple times a frame, or none at all.
     * Use this function to move any physical objects (e.g. If you have a RigidBody attached to a GameObject, put your rigidBody.Move here).
     * To keep track of time passed, use Time.fixedDeltaTime.
     */
    void FixedUpdate()
    {
    }
 
    /**
     * This is the Collision Section.  Uncomment a #define at the top if you want this Behaviour to handle collision events.
     * 
     * Rules for Collisions:
     * 
     *     1. The collider must be attached to the same GameObject this Component is on (e.g. no listening to the parent).
     *     2. One of the colliders (this GameObject, the other, or both) must have a RigidBody (e.g. can't manually move one into another and expect this to work).
     *  3. You should only use Solid Collisions OR Trigger Collisions, not both.  If you need both, make two separate GameObjects with a common parent.
     */
#if USE_SOLID_COLLISION && USE_TRIGGER_COLLISION
    #error You just violated Rule #3.  You should only be using Solid Collision OR Trigger Collision, not both.
#elif USE_SOLID_COLLISION
    /**
     * OnCollisionEnter is called whenever contact first happens with another collider.
     */
    void OnCollisionEnter(Collision collisionInfo)
    {
    }
     
    /**
     * OnCollisionExit is called whenever one collider stops touching another collider.
     */
    void OnCollisionExit(Collision collisionInfo)
    {
    }
     
    /**
     * OnCollisionStay is called throughout the duration of contact with another collider.
     */
    void OnCollisionStay(Collision collisionInfo)
    {
    }
#elif USE_TRIGGER_COLLISION
    /**
     * OnTriggerEnter is called when one collider first enters the volume of a trigger collider.
     */
    void OnTriggerEnter(Collider otherCollider)
    {
    }
     
    /**
     * OnTriggerExit is called when one collider exits the volume of another collider.
     */
    void OnTriggerExit(Collider otherCollider)
    {
    }
     
    /**
     * OnTriggerStay is called when on collider is inside the volume of another collider.
     */
    void OnTriggerStay(Collider otherCollider)
    {
    }
#endif
}

Leave a Comment