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 matthewbauer · Aug 02, 2014 at 08:51 PM · instantiatecoroutinetime

How to wait for Start() to run when calling InstantiateObject()

I'm having a problem with timing. When I instantiate an object I add it to the map using the object's Start() method. So, in some parts of my code, I need the start method to be ran before I can continue with the method. Or else I get in the situation I'm in now where multiple trees are being put on the same tile because they don't know it's already occupied.

I know I could use a coroutine to wait for an amount of time to pass, but it would be better if I didn't have to guess at how long the instantiation will take.

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

1 Reply

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

Answer by iwaldrop · Aug 02, 2014 at 09:25 PM

Unity's lazy loading of objects via the Start method is fine in most cases. If you can't simply move that logic to Awake, then it sounds like need tighter control over your object creation cycle. Here's what I do:

  1. Use Prefabs. They allow you to setup defaults on objects which can then be modified in the object creation process.

  2. Use Constructor-like methods to build your objects. Here, you will pass in the data that you need and know that the object is setup by the time the method completes. This looks something like this:

    public SomeObject CreateObject(ObjectManifest manifest) { SomeObject result = null; if (manifest != null) { GameObject go = GameObject.Instantiate(prefab) as GameObject; result = go.GetComponent(); result.SetInfo(manifest); } return result; }

    public class SomeObject : MonoBehaviour { public SetInfo(ObjectManifest manifest) { // apply all settings from the manifest to the object, ex: result.enabled = manifest.startEnabled;

          // or pass nothing and do your Start() stuff here
         }
     }
    
    

Again, doing it this way will ensure that your object is fully initialized by the time you get the reference back. Note that I use the term 'manifest' loosely, and that it is an abstraction for the notion of passing in some configuration data, be it in the form of a custom class or a bunch of method arguments.

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 iwaldrop · Aug 02, 2014 at 09:27 PM 0
Share

I'm not sure why the code formatting isn't working...

avatar image matthewbauer · Aug 02, 2014 at 09:55 PM 0
Share

Thanks! I didn't even know there was an Awake method, oops. I put my logic in there and it works perfectly. I appreciate the response.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

instantiating at a random time 1 Answer

continuous shooting 1 Answer

Help Guitar Hero Style or similar 0 Answers

Respawn Player Using a Coroutine 0 Answers

Using coroutine to change current GUI 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