Scripting
Table of Contents
- Execution Order of Event Functions
 AwakevsStartHideInInspectorvsNonSerializedvsSerializeField- Serialization
 - Don't define a Coroutine named 
Main SendMessageOnBecameVisible/OnBecameInvisibleObjectGameObject- Coroutine
 - C# classes need to have its own script with the class' name?
 
Execution Order of Event Functions discussion

Awake vs Start discussion
AwakeandStartare called only once in thier lifetimeAwakeis called when the script instance is being loaded(whther or not the scriptenabled)Startis called on the frame when a script is enabled(beforeUpdate)Awakeis called on all objects in the scene before any object'sStartis called.
HideInInspector vs NonSerialized vs SerializeField discussion

- Things can only be visible in the inspector if they are serialized.
 - If something is not serialized it will never show up in the inspector.
 HideInInspectorattribute make it invisible in the inspector but still allow each instance of a SerializeField to hold its own default value.
HideInInspector
SerializeField
public class SomePerson : MonoBehaviour
{
    //This field gets serialized because it is public.
    public string name = "John";
    //This field does not get serialized because it is private.
    private int age = 40;
    //This field gets serialized even though it is private
    //because it has the SerializeField attribute applied.
    [SerializeField]
    private bool hasHealthPotion = true;
}Serialization discussion
Many of unity features build ontop of the serialization system:
- Storing data stored in your scripts
 - Inspector window
 - Prefabs
 - Instantiation
 - Saving and loading scenes
 - Hot reloading of editor code
 - Resource.GarbageCollectSharedAssets()
 
[Serializable]
class Animal
{
    public string name;
}
class MyScript : MonoBehaviour
{
    public Animal[] animals;
}Don't define a Coroutine named Main discussion
Don't define a coroutine named Main.
I wrote a coroutine named Main. It executed twice, in an unexpected way.
I've tweaked many times to understand why. Main executed even I didn't explicitly call StartCoroutine.
At last, I renamed Main to Main2, and it worked all the way I expected.
SendMessage reference
- Calls the method named 
methodNameon everyMonoBehaviourin theGameObject. - It's not possible to get a return value from 
SendMessageorBroadcastMessage 
OnBecameVisible / OnBecameInvisible reference
- When running in the editor, scene view cameras will also cause this function to be called
 OnBecameInvisibleis only called the object had been visible
Object reference
Instantiate
- If cloning a 
Componentthen theGameObjectit is attached to will also be cloned - All child objects and components will also be cloned
 
// Preferred
public static T Instantiate(T original);
public static T Instantiate(T original, Transform parent);
public static T Instantiate(T original, Transform parent, bool worldPositionStays);
public static T Instantiate(T original, Vector3 position, Quaternion rotation);
public static T Instantiate(T original, Vector3 position, Quaternion rotation, Transform parent);
// 'as T' required for type
public static Object Instantiate(Object original);
public static Object Instantiate(Object original, Transform parent);
public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);Instantiated GameObject
- Before Instantiate
 - Awake of Instantiated GameObject
 - After Instantiate
 - Start of Instantiated GameObject
 
Destroy
- If obj is a 
Componentit will remove the component from theGameObjectand destroy it. - Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.
 
GameObject reference
Create Empty with Code
Transformis always added to the GameObject that is being created.
GameObject player;
player = new GameObject("Player");
player.AddComponent<Rigidbody>();
player.AddComponent<BoxCollider>();FindWithTag
Coroutine reference
- Coroutines have virtually no performance overhead.
 StartCoroutinefunction always returns immediately,yield return <something>to make coroutine resume after<something>yield breakto stop the coroutine- SEE: Execution Order
 
C# classes need to have its own script with the class' name? discussion
- Only 
MonoBehavior,ScriptableObject, and the children of either MUST have their own files - Unity magic (adding classes as Components, etc..) requires the class be alone in a file, with the same name.