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
2
Question by hexdump · Nov 24, 2014 at 12:02 AM · instantiateinstantiationinitializationinitialize

Instantation and use of gameobject in same frame

Hi,

I would like to know how you deal with a scenario like this:

  1. When a mouse button is clicked a prefab is instantiated.

  2. This prefab have some components that need initialization and the initialization code is placed in awake and start.

  3. Right after creation (same frame) the gameobjects are passed to a function that will do some calculatiosn with it.

You see the problem. The initialization hasn't been ran for the different components in the game object. So, 3 won't work or will render a wrong result.

Solutions I have found (thought about):

  1. After creation wait for next frame with a coroutine (WaitForEndOfFrame) and then, execute step 3(pass this objects now that awake and start has been called to the worker function).

  2. Instantiate the game object and them add it to a list. After all frame work is done when LateUpdate() callback of the behaviour that created the gameobject is called, test if there are newly created gameobjects and pass all these newly created objects to the worker function.

  3. Move initialization code to different inits in each behavour and initialize then manually (really bad aproach).

Would you mind commenting any other way to accomplish this?

Cheers.

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 Kiwasi · Nov 24, 2014 at 12:13 AM 1
Share

Not 100% sure, but I believe Awake is called as part of Instantiate. So anything initialised there will be available for your next line of code. Worth double checking this.

Start will not run until before Update starts in the next frame, so is not overly helpful to you.

avatar image hexdump · Nov 24, 2014 at 12:16 AM 0
Share

You are totally right. Didn't know how I could miss this. Awake is called through the instantiation process.

2 Replies

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

Answer by Kiwasi · Nov 24, 2014 at 08:55 AM

Just ran a quick test in Unity to confirm. Awake is called before the next line of code, so long as the prefab is an active GameObject.

Here are the scripts and the results.

 public class FunctionTestManager : MonoBehaviour {
     public GameObject testPrefab;
 
     void Start () {
         Debug.Log ("Before Instantiate");
         Instantiate (testPrefab);
         Debug.Log ("After Instantiate");
     }
 }
 
 
 public class FunctionTest : MonoBehaviour {
 
     void Awake (){
         Debug.Log ("Awake");
     }
 
     void Start () {
         Debug.Log ("Start");
     }
 }

Console reads

  • Before Instantiate

  • Awake

  • After Instantiate

  • Start

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 Kiwasi · Nov 24, 2014 at 08:56 AM 0
Share

Accepted this as OP has indicated this worked in comments.

avatar image
0

Answer by AlwaysSunny · Nov 24, 2014 at 12:15 AM

This type of open-ended question is better suited to the forums.

If you can wait till next frame to use the object and let start/awake handle initialization, I guess that's fine. I personally prefer custom Inits to all that rigamarole. It really varies case by case which is more appropriate, but I find Inits much cleaner. If you inherit from a base class and override a virtual (blank) Init, you don't even need to have the script object as its derivative type. I have manual Inits in about a quarter or third of my scripts. They're not that bad to work with.

Comment
Add comment · Show 3 · 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 hexdump · Nov 24, 2014 at 12:24 AM 0
Share

One of the things I don't like about the multi init thing is that retrieving a component is an expensive operation. Doing this, components would need to be requested x2. One pass when you manually have to do manual initialization and another one when unity needs to get them to set values, etc... It feels a bit dirty to me. And if I would have to do this I would create an "Entity Controller" that would be the one that now what components to init and how. For me, this smells like good old inheritance based initialization more than component based system initialization. Could be wrong though.

avatar image AlwaysSunny · Nov 24, 2014 at 12:37 AM 0
Share

You're right that it's going to create some waste and some spaghetti. I've not found a better way, though. Each project I work on gets a highly generic inherit-from-me class that includes lazy-getters for project-specific components to ease some of the burden. That's more for dev convenience than performance, though.

Also I'm sure you're aware that if you need to get a component twice in the same scope, you can store a scope-level reference and use GetComponent only once. I assume the garbage that creates is superior to the waste of two GetComponent calls.

avatar image hexdump · Nov 24, 2014 at 12:40 AM 0
Share

After some test (Unity was not compiling my scripts) seems that awake is called in the Instantiate process as Bored$$anonymous$$ormon suggested. This is all I need :).

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

28 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

Related Questions

Initializing an object on Network.Instantiate 0 Answers

Why is it important to create an empty gameobject for my prefabs? 0 Answers

Cloud objects not instantiating (Oh no!). 0 Answers

Instantiate as child of a to-be-instantiated list item 1 Answer

How to limit the instantiation of a prefab/object? 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