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 Saryk360 · May 16, 2018 at 03:19 AM · instantiatedelayreturn

When does Instantiate return ?

I have a Loader script that basically instantiates the GameManager singleton if it doesn't already exist. Thing is, I have a blank Scene at the start to make all the loading, and I want to load the next scene when the GameManager is done doing its things. I have the following code :

 public class Loader : MonoBehaviour
 {
     public GameManager gameManager;
 
     void Awake()
     {
         if (GameManager.instance == null)
         {
             Instantiate(gameManager);
             gameManager.LoadNextScene();
         }
     }
 }

And this is the GameManager :

 public class GameManager : MonoBehaviour {
 
     public static GameManager instance = null;
     private XMLReader xmlReader;
 
     private void Awake()
     {
         if (instance == null)
             instance = this;
         else if (instance != this)
             Destroy(gameObject);
         DontDestroyOnLoad(gameObject);
         xmlReader = GetComponent<XMLReader>();
         Initialize();
     }
 
     private void Initialize()
     {
         xmlReader.Reader();
     }
 
     public void LoadNextScene ()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
     }
 }

I want to stay on the first Loading Scene until the Reader() function is done, and I don't know if this is working right now.

When does Instantiate() return ? Is it when the code in the instantiated object's Awake returns ? Is it before even calling Awake on the object ?

If it is the latter, how to delay a function call until another function is finished with its execution ?

Comment
Add comment · Show 1
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 Saryk360 · May 16, 2018 at 05:40 PM 0
Share

I edited the question to be clearer in the actual question I'm curious about. I want to know when Awake returns to know if what I'm doing would work, or if I need to get into other solutions ?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Captain_Pineapple · May 16, 2018 at 05:36 AM

Hey there,

for your loading problem you should perhaps look into Asynchronous Scene Loading. That might solve your problems, you can still do stuff while loading and you can see when its finished by following their code example in the docs.

For delaying a function call i mostly just set a flag in the said function call and check for that flag in for example the update fuction. Though there are probably better ways.

Hope this helps.

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 Saryk360 · May 16, 2018 at 06:04 PM 0
Share

I know about asynchronous loading, but here I don't want to preload Scene2 ; I want to make sure I'm done loading data from the xml file in my game manager in Scene1 before going into Scene2, because Scene2 needs the data to be there.

avatar image
0

Answer by sum1nil · May 16, 2018 at 05:38 AM

@Saryk360

This may be of help with the second part of your question.

  private void Initialize()
  {
     private static object lockObject = new object();
 
     lock(lockObject) {
         // ... code goes here...
         xmlReader.Reader();
     }
  }

https://stackoverflow.com/questions/1897265/blocking-a-function-call-in-c-sharp

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 Saryk360 · May 16, 2018 at 06:17 PM 0
Share

Can I use that object to lock conflicting calls to different functions ?

i.e. lock the object when I call Reader and prevent LoadNextScene to be called when the object is locked ?

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

84 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

Related Questions

How to Instantiate prefabs with function Update? 1 Answer

Delay Collecting Object by Player 0 Answers

how to make an enemy fire a bullet once every second 4 Answers

Have a function wait unitl a boolean is false 1 Answer

How to WAIT for RPC to return/finish? 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