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
1
Question by Lockstep · Oct 21, 2012 at 01:02 PM · instantiatechildexecution order

Weird execution order when instaniating a gameObject with too many children

Hello. I wanted to create a gun which shoots multiple projectiles at once in a fan pattern. To save Instantiate calls, instead of instantiating every single projectile I rather instantiated one gameObject which had all the projectiles as a child object. The scripts on the gun, parent projectile and child projectiles worked well for three and five child projectiles.

However when I tried to fire seven projectiles only two of them were actually moving. The other five just remained at the start position. Apperently the Start function of the idle projectiles is called before the Start of the parent projectile (see below for details). I'm a little clueless about why this happend, since the scripts worked fine with a lower childcount. Also I did not get any error message.

Those are the relevant parts of my scripts:

Gun script:

 //this is inside of the shoot function 
         var instantiatedProjectile : Transform = Instantiate (projectile, shotPoint.position, transform.rotation);
         var spreadScript : FireSpread = instantiatedProjectile.GetComponent(FireSpread);
         spreadScript.ignoreMe = transform.root.collider;
         spreadScript.initVelocity = shotPoint.TransformDirection(direction.normalized * projSpeed);
         spreadScript.spreadAngle = spreadAngle;

parent projectile script:

 function Start () {
     var childs : int = transform.childCount;
         var children : smallSpread[];
         children = transform.GetComponentsInChildren.<smallSpread>() ;
     for( var j : int = 0 ; j< childs ; j ++){
         var turnAngle : float = -(spreadAngle/2) + (j + 0.0) * spreadAngle / (childs - 1.0);
         Debug.Log(j + " angle " + turnAngle);
         var targetVelocity : Vector3 = Quaternion.AngleAxis(turnAngle, transform.up) * initVelocity;
         Debug.Log("velo " + targetVelocity);    //inidcates that the velocity is calculated correctly
         children[j].initVelocity = targetVelocity;
     }
 }

child projectile script:

 function Start (){
     rigidbody.velocity = initVelocity;
     Debug.Log("actuall initVelocity " + initVelocity);    //behaves weird see below
 }

The Debug.Log on the child projectile behaves pretty weird. The first five of the seven Logs are printed before the Logs of the parent projectile. Those Logs all give out (0,0,0) as velocity. The remaining two Logs are printed after the Logs of the parent projectile and show the correct velocity.

It seems like the Start functions of a part of the children are called before the parents Start and the other part is called afterwards. I am totally confused why this is happening. Please enlighten me. Thank you.

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
2
Best Answer

Answer by Loius · Oct 21, 2012 at 04:22 PM

The same Unity function across multiple GameObjects are called in random order. All the Starts, Awakes, and Updates will just run whenever. But all Awakes run before all Starts, which run before all Updates. You could swap the parent's Start to Awake.

If a child object relies on its parent for anything other than transform inheritance, it's always a good idea to explicitly initialize the children once the parent is aware of them. If you have the parent call 'Initialize' on the children, and rename the child's Start to Initialize, you shouldn't have any issues.

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 aldonaletto · Oct 21, 2012 at 04:38 PM 1
Share

Awake may not work because the children may not have been created yet, but the Initialize idea is ok. Rename and modify the Start function in the projectile script as follows:

 function Initialize(vel: Vector3){
   rigidbody.velocity = vel;
 }

and modify the assignment line in the parent script as follows:

   ...
   children[j].Initialize(targetVelocity);
   ...

You won't need the initVelocity anymore, since the projectile velocity will be set directly by Initialize.

avatar image Lockstep · Oct 22, 2012 at 08:47 PM 0
Share

Thank you for your replies. In the mean time i solved the problem by yielding the start of the child object. This makes sure that the child's start is called after the parent's. This does the trick but your suggestion seems to be the cleaner way of handeling things. I will definitely use this when i'm in a similar situation.

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

Prefab, procedural attachment of child, childCount return 0 1 Answer

Instantiating prefab at child (spawnlocations are arrays) 2 Answers

How to find and remove the last child of parent? 1 Answer

Instantiate as child of target object 1 Answer

access child of a gameobject 7 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