- Home /
Loop through Children Unable to Access Component Script
In the space shooter I'm designing, a dead ship spawns a bunch of ship fragments, which are supposed to drift apart and then disappear after a short time. To do that for my Slicer fighter, I have two scripts - SlicerFragElement, which is assigned to each of the child fragments, has velocity and angular velocity fields, and simply handles translating the frag it is assigned to. To set this up, I have the SlicerFragElement script, which is supposed to assign velocities to all of the individual frags. To do that, I have the following code (print statements are for debugging):
var startTime : float;
var duration : float = 5.0;
function Start () {
startTime = Time.time;
//*
for (var child : Transform in this.transform) {
var distanceVec : Vector3 = child.position - transform.position;
//print(distanceVec);
//print(child.name);
child.GetComponent("SlicerFragElement").velocity =
Random.Range(5.0, 14.0) * (1 / Mathf.Pow(distanceVec.magnitude + .2, 2)) * distanceVec.normalized;
//print(child.GetComponent("FalconFragElement").angularVel);
}
}
function Update () {
if (Time.time > startTime + duration) {
Destroy(this.gameObject);
}
}
The problem is, this code cannot seem to modify the SlicerFragElement scripts for any of the children. Their velocity stays at 0, the print statements show (0,0,0,0) and (0,0,0) for the angularVel and velocity, even when the angularVel (assigned in SlicerFragElement ) is nonzero. However, the names do show up correctly.
Any idea what the problem is? Also, would just assigning rigidbodies to each of the frag elements be more processor-efficient (there may be hundreds of these onscreen at once, so that's a concern)?
Answer by Programmer-Tau · Nov 22, 2013 at 05:38 AM
Without the code for SlicerFragElement, it's kind of hard to say.
My first suggestion is to simply set the velocity to some constant value, if you haven't tried that already. If it's still always zero, then I would suspect something in the SlicerFragElement code that is resetting it to zero.
At a much higher level though, you may want to use Unity's particle system (Shuriken) for something like this anyway. If there's no gameplay element to the fragments, then using Unity's particle system will be much more efficient, and is less work in the long run.
Particles FTW. Also, they're probably going to be cheaper than a bunch of geometry, unless your geo is less than 4 verts.
So how would you use particles for this? Bear in $$anonymous$$d that the fragments were obtained by manually splitting apart the fighter mesh in Blender, so each fragment is unique.
Also, here's the code for SlicerFragElement: var velocity : Vector3 = Vector3.zero; var angularVel : Quaternion;
function Start () {
angularVel = Random.rotation;
}
function Update () {
transform.Translate(velocity * Time.deltaTime, Space.World);
var rotTarget = transform.rotation * angularVel;
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotTarget, 20 * Time.deltaTime);
}
It's pretty simple, which is why I didn't post it earlier.
Hmm, yeah, it looks pretty straight forward, and I don't see anything that would keep velocity and rotation at zero. I assume you've checked this, but are you sure Start and Update are being called on SlicerFragElement? Sorry I wasn't much help here.
Regarding the use of particles, if you want to be physically accurate and use fragments you created from Blender, then your approach is the right way. If you use particles, you do need to limit the kind of geometry you spawn. With that said, if you're spawning hundreds in the screen, you'll be spending a lot of draw calls and CPU simulation time on what sounds like a cosmetic effect.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122447im_/https://answers.unity.com/themes/thub/images/avi.jpg)