# Awake và Start

<table id="bkmrk-ti%C3%AAu-ch%C3%ADawake%28%29start"><colgroup><col style="width: 130px;"></col><col></col><col></col></colgroup><tbody><tr><th>Tiêu chí

</th><th>****Awake()****</th><th>****Start()****</th></tr><tr><td>****Thời điểm gọi****

</td><td><span style="white-space: pre-wrap;">Ngay khi </span>****script được nạp/khởi tạo****<span style="white-space: pre-wrap;"> (khi scene load hoặc </span>`<span class="editor-theme-code">Instantiate</span>`<span style="white-space: pre-wrap;"> prefab)</span>

</td><td>****Khung hình đầu tiên****<span style="white-space: pre-wrap;"> khi component </span>****được enable****<span style="white-space: pre-wrap;"> sau </span>`<span class="editor-theme-code">Awake</span>`/`<span class="editor-theme-code">OnEnable</span>`

</td></tr><tr><td>****Yêu cầu enable****

</td><td><span style="white-space: pre-wrap;">Gọi </span>****dù component bị Disable****

</td><td><span style="white-space: pre-wrap;">Chỉ gọi khi </span>****component Enable****

</td></tr><tr><td>****Mục đích chính****

</td><td><span style="white-space: pre-wrap;">Khởi tạo </span>****nội bộ****<span style="white-space: pre-wrap;">: tạo dữ liệu, tìm &amp; cache reference trong </span>****cùng GameObject****

</td><td><span style="white-space: pre-wrap;">Khởi tạo </span>****phụ thuộc ngoại****<span style="white-space: pre-wrap;">: cần đảm bảo </span>****các đối tượng khác đã Awake xong****

</td></tr><tr><td>****Thứ tự giữa các object****

</td><td>****Không đảm bảo****<span style="white-space: pre-wrap;"> thứ tự Awake giữa các object khác nhau</span>

</td><td>****Không đảm bảo****<span style="white-space: pre-wrap;"> thứ tự Start giữa các object khác nhau</span>

</td></tr><tr><td>****Gọi lại khi Instantiate****

</td><td><span style="white-space: pre-wrap;">Có (mỗi lần </span>`<span class="editor-theme-code">Instantiate</span>`)

</td><td>Có (khi instance được enable và tới frame đầu)

</td></tr><tr><td>****Best practice****

</td><td>Cache component (`<span class="editor-theme-code">GetComponent</span>`), set giá trị mặc định, đăng ký dịch vụ/DI

</td><td>Kết nối hệ thống khác, subscribe event ngoài, logic cần đảm bảo đối tượng khác đã sẵn sàng

</td></tr><tr><td>****Lưu ý****

</td><td>Gọi API phụ thuộc đối tượng khác chưa sẵn sàng

</td><td><span style="white-space: pre-wrap;">Công việc nặng mỗi frame; tránh dùng nếu chỉ cần ở </span>

`<span class="editor-theme-code">Awake</span>`

</td></tr></tbody></table>