Teaching Newbies since 2014

kauress

  • Home
  • Javascript ❤
    • JavaScript
    • Node.js
  • WebDev Tuts
  • screencasts
  • Resources
  • VR & AR
  • Contact
  • Github
  • Twitter
  • YouTube
  • RSS
  • C++
You are here: Home / Unity3D / Unity3D Scripting API #8: OnEnable() 🕹️

January 30, 2021 by: Kauress

Unity3D Scripting API #8: OnEnable() 🕹️

⭐ OnEnable():

⭐Event methods get called automatically by the Unity3D engine at pre-determined times

 🕹️Called after Awake()                             
🕹️Called whenever the script component is enabled which can be more than once      
🕹️ Usage: Resetting variables and current object properties. Or for executing code when the script component is enabled    
🕹️ Start() is called once, OnEnable() is called every time the script component, or the object it’s attached to is enabled.    
🕹️ OnEnable() isn’t suitable for initialization tasks that only need to happen once.

Order of execution of Awake(), Start() and OnEnable()

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Start");
    }
 void Awake()
    {
        Debug.Log("Awake");
    }

 void OnEnable()
    {
        Debug.Log("On Enable");
    }
}
Order of execution

☀️ Usage: Awake() is used to initialize any variables, properties, or game state of an object before the game starts

🍩Start(): This method is called on the first frame of the game before

🕹️ OnEnable(): Called whenever the script component is enabled (can be more than once)

Introduction to C# in Unity3D #3: Data Types 🌌
Unity3D Scripting API #9: OnDisable() 🛡

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2021 ·Kauress