Scripting
Table of Contents
- Execution Order of Event Functions
Awake
vsStart
HideInInspector
vsNonSerialized
vsSerializeField
- Serialization
- Don't define a Coroutine named
Main
SendMessage
OnBecameVisible
/OnBecameInvisible
Object
GameObject
- Coroutine
- C# classes need to have its own script with the class' name?
Execution Order of Event Functions discussion
Awake
vs Start
discussion
Awake
andStart
are called only once in thier lifetimeAwake
is called when the script instance is being loaded(whther or not the scriptenabled
)Start
is called on the frame when a script is enabled(beforeUpdate
)Awake
is called on all objects in the scene before any object'sStart
is 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.
HideInInspector
attribute 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
methodName
on everyMonoBehaviour
in theGameObject
. - It's not possible to get a return value from
SendMessage
orBroadcastMessage
OnBecameVisible
/ OnBecameInvisible
reference
- When running in the editor, scene view cameras will also cause this function to be called
OnBecameInvisible
is only called the object had been visible
Object
reference
Instantiate
- If cloning a
Component
then theGameObject
it 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
Component
it will remove the component from theGameObject
and 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
Transform
is 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.
StartCoroutine
function always returns immediately,yield return <something>
to make coroutine resume after<something>
yield break
to 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.