Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by thenachotech1113 · Jan 02, 2013 at 12:31 AM · startenabledawake

does finction start or awake run when the object or script is enabled mid game

i want to knwo if function Start and/or function awake when the script is enabled mid game from an other script?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
38
Best Answer

Answer by Bunny83 · Jan 02, 2013 at 01:13 AM

Yes, they are called mid game. Basically there are some different situations:

  • 1 The game object is active but the script is disabled.

  • 2 The game object is deactivated but the script is enabled.

  • 3 The game object is deactivated and the script is disabled.

  • 4 The game object is active and the script is enabled.

In general there's no difference between the behaviour of objects in a scene and those created at runtime with Instantiate.

In case 1 when the object is created only Awake is called immediately when the object is created. OnEnable and Start will be called when you enable the script.

In case 2 when the object is created nothing is called. When you set the game object active, Awake will be called followed by OnEnable and Start

In case 3 when the object is created nothing is called. When you set the game object active, only Awake will be called. If you enable the script later OnEnable and Start will be called.

In case 4 when the object is created Awake will be called followed by OnEnable and Start.

Keep in mind that all Start calls have a fix position in the game loop. The Start call is always delayed until the next frame. OnEnable and Awake will be called immediately when appropriate. That means in case 4 when you call Instantiate to create an object in game, Awake and OnEnable will be called before the Instantiate call returns.

edit
Just in case that fact isn't cleat:

Awake and Start is called only once in the lifetime of an object. So once it has been activated / enabled once Start and Awake won't be called again. OnEnable (and OnDisable) will be called each time it is enabled / disabled.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 02, 2013 at 01:33 AM 1
Share

BTW OnEnable and OnDisable are linked with the active state of the game object as well. So OnDisable will be called when you either deactivate the GameObject or when you disable the script.

So OnEnable will be called when this condition becomes true:

 (gameObject.active && enabled)

OnDisable will be called when the condition becomes false.

avatar image
2

Answer by spritesobhan · Apr 30, 2017 at 10:19 AM

So Awake is called when GameObject is Active, even if Script is disabled. Start is called only when both are Active.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by ballz · Feb 02, 2018 at 03:27 AM

 when do MonoBehaviour callbacks 
    A = awake()    E = OnEnable()    S =  Start()
 run with respect to active & enabled states?
                         
  class                    created       activate      enabled
 active : enabled           A->E->S              
 active : disabled          A              E ->S
 deactivated:enabled       nothing         A->E->S
 deactivated:disabled      nothing         A            E->S
 
 NB:
 Enable / Disable are called many.
 A & S are called only once in lifecycle. 
 A & E calls will interleave in enabled classes
 execution order of scripts can be assigned in Edit | Project Settings

other observations

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ImPHL1 · Feb 12, 2020 at 04:04 PM 0
Share

line 7 should be like this:

 active : disabled      A       -        E -> S

because game object is already activated and its script that it's disabled so you can enable it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Something like Awake that runs even if disabled? 11 Answers

DontDestroyOnLoad() not calling awake/start again 2 Answers

Object reference becomes null between Awake and Start after scene load 2 Answers

Why wouldn't Debug.Log show up in Start or Awake ? 3 Answers

Component's Start/Awake methods not being called 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges