Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by sarneeh · Feb 15, 2018 at 05:24 PM · instantiatestartinitializationrace

Race conditions using Instantiate

Hey!

I have a question because I can't understand how does it work actually.

Let's say I've got this code:

PlayerFactory:

 public class PlayerFactory : MonoBehaviour
 {
     [SerializeField]
     private GameObject _playerPrefab;
 
     public Player Create()
     {
         GameObject gameObject = Instantiate(_playerPrefab);
         Player player = gameObject.GetComponent<Player>();
 
         var someInitVariable = 5;
         player.Initialize(someInitVariable);
 
         return player;
     }
 }

Player:

 public class Player : MonoBehaviour
 {
     private int _initVariable;
 
     public void Initialize(int someInitVariable) 
     {
         _initVariable = someInitVariable;
     }
 
     private void Start() 
     {
         SomeStaticManager.DoSomething(_initVariable);
     }
 }

Please explain me, having this example - why the Start method is not called before the Initialize method? I thought, that after using Instantiate, the GameObject will be instantiated on the scene and immediately started - so I've expected a race condition here, but there's none. Why? I'd really appreciate any insight into this because I'd like to understand how it really works.

Thanks in advance!

Comment
Add comment · Show 2
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 TreyH · Feb 15, 2018 at 05:35 PM 1
Share

$$anonymous$$ight help to read the docs on Start and Awake: https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.Start.html


Start will not be called if your prefab starts off disabled, btw. Also, when is Create being called?

avatar image sarneeh TreyH · Feb 15, 2018 at 05:50 PM 0
Share

Create is being called in other script's Start method.

1 Reply

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

Answer by Bunny83 · Feb 15, 2018 at 08:18 PM

If you read the documentation of the Start method the first sentence reads:

Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.


So Start will be called right before the next Update should be called. If your object is instantiated during Update, Start of the newly created object will be called the next frame. If your object is instantiated inside another classes Start method, the Start method of the new object may be executed this or next frame. However Start is never called from inside Instantiate unlike Awake. OnEnabled and Awake is called before Instantiate returns (at least when the gameobject is instantiated active and enabled).


Unity's scripting environment is single threaded. Since Start isn't called inside any of the methods you use in your Create method Start can only be invoked by the engine once your Create method (and the caller method is finished).

It's actually quite difficult to actually get a race condition in Unity. There may be ordering problems but never real race conditions as they require at least two threads.

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 sarneeh · Feb 16, 2018 at 08:23 AM 0
Share

This. What actually made me understand why it works like that is that Unity is single threaded. Don't know why, but I always thought that it works in multiple threads. That's why I thought that in the initialization a lot of things is working in the background (like instantiating and running all the startup callback commands: Awake, Start, etc.).

Thank you for your explanation! :)

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

95 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

While Loop Not Assigning Array 1 Answer

Countdown To Start Game 2 Answers

Awake called after I activate object, not after Instantiate... is it normal?? 2 Answers

Strange Race condition for one prefab 1 Answer


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