- Home /
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.
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613082710im_/https://answers.unity.com/themes/thub/images/avi.jpg)