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
0
Question by jss367 · May 22, 2015 at 02:26 AM · awake

How to initialize after Start()

I would like to run another initialize command after Start() but I do not know the best way to do this. I want to do something like this:

void Awake() { //Set up the environment }

void Start() { // Prepare the variables for the environment }

void LateStart() { // Ask the other scripts about their variables }

I can't ask the other scripts about their variables until after Start or I will run into a race condition (where whether it errors or not will be based on the sequence the Start() functions are called). I can add a toggle to Update() so the first time it updates it runs the LateStart() commands, but I don't think this is the best way. Any ideas?

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

4 Replies

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

Answer by Mehul-Rughani · May 22, 2015 at 04:08 AM

Another Way To Do This is...

 void Start()
     {
         StartCoroutine(LateStart("Time You Want To Wait"));
     }
 
     IEnumerator LateStart(float waitTime)
     {
         yield return new WaitForSeconds(waitTime);
         //Your Function You Want to Call
     }


Hope It Will Helps You..

Comment
Add comment · Show 2 · 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 Hsni · Oct 11, 2018 at 06:41 AM -1
Share

using Invoke ins$$anonymous$$d of Coroutine makes more sense here.

avatar image Legend_Bacon · Oct 11, 2018 at 09:00 AM 2
Share

Since this thread has already been revived...

You can also use $$anonymous$$ain(). It is automatically called between Awake() and Start().

Cheers, ~LegendBacon

avatar image
10

Answer by pjbaron · May 09, 2018 at 02:56 PM

There is a built-in system to control the order in which things are processed:

Edit | Project Settings | Script Execution Order

Here you can adjust each script up or down in a list, so that they process in the specified order and either before or after the usual 'default time'.

In the OP case it will fix the race conditions provided that none of the references end up being circular (object X has variable y referencing object Y, and object Y has variable x referencing object X). In these cases it is necessary to make a public setter function for one of them and call it from the other.

Unfortunately this feature is pretty much hidden away, so it isn't a great solution if you intend to share your project at all... Also depending on a little known feature to make your system work feels bad to me, but I think it is less nasty than delayed coroutine initialisation which will probably end up with your Update being called before your LateStart.

Comment
Add comment · Show 2 · 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 geganadiradze · Jul 24, 2019 at 08:34 AM 0
Share

I believe this is the best solution for given problem. Also if anyone is wondering where are these settings saved - it looks like this settings are saved in .meta file of given script.

avatar image koraken · Aug 15, 2020 at 07:47 PM 0
Share

This should be the correct answer. The root problem is that the scripts are not starting in the correct order. Delaying the execution with a co-routine, as the current selected answer suggests, is a bandaid on top of the actual problem.

avatar image
0

Answer by chintan_shroff · May 22, 2015 at 09:20 AM

You could use coroutines in your start method and define the delay you want http://docs.unity3d.com/ScriptReference/Coroutine.html

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
0

Answer by gglobensky · Jun 16, 2017 at 02:52 AM

In a Coroutine there are also WaitForFixedUpdate or WaitForEndOfFrame that could help you. If you wanna do it time based you could invoke your method. That'd be simpler than a Coroutine in that case.

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

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

11 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

Related Questions

Awake Method Called Twice 1 Answer

How to reference GameObject in Awake or Start 2 Answers

Class c = new Class() okay in class body? 1 Answer

have an enemy change position on the y axis when awake (2d platformer) 0 Answers

How to mock or skip execution of Awake in playmode unit tests? 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