- Home /
How to create an array out of many gameObjects?
So I have 12 Particle Emitters which are drag-n-droped onto inspector and I want to make an array out of them to make Unity cache them in memory (I suppose it works that way?). Code looks like this and works quiet slow (lags for half second by 5-10 sec step)
var ShipEnergyVar : float = 100.0;
var Destroyer : GameObject;
var LeftCollisionRay = Vector3(0,0,0); private var LeftCollisionRayDir = Vector3(-0.05,0,2);
var RightCollisionRay = Vector3(0,0,0); private var RightCollisionRayDir = Vector3(0.05,0,2);
var L_Particle1 : ParticleEmitter; var LS_Particle1 : ParticleEmitter;
var L_Particle2 : ParticleEmitter; var LS_Particle2 : ParticleEmitter;
var L_Particle3 : ParticleEmitter; var LS_Particle3 : ParticleEmitter;
var R_Particle1 : ParticleEmitter; var RS_Particle1 : ParticleEmitter;
var R_Particle2 : ParticleEmitter; var RS_Particle2 : ParticleEmitter;
var R_Particle3 : ParticleEmitter; var RS_Particle3 : ParticleEmitter;
function ShipEnergy(){
Speed = rigidbody.velocity.magnitude * 0.003;
if (Physics.Raycast (transform.TransformPoint (LeftCollisionRay), transform.TransformDirection (LeftCollisionRayDir), Hit, 2, layerMask)){
ShipEnergyVar -= Speed;
if (Hit.distance < 0.4){
L_Particle1.emit = true; LS_Particle1.emit = true;
}
if (Hit.distance > 0.6 && Hit.distance < 1.2){
L_Particle2.emit = true; LS_Particle2.emit = true;
}
if (Hit.distance > 1.2){
L_Particle3.emit = true; LS_Particle3.emit = true;
}
}else {L_Particle1.emit = false; LS_Particle1.emit = false;L_Particle2.emit = false; LS_Particle2.emit = false;L_Particle3.emit = false; LS_Particle3.emit = false;}
if (Physics.Raycast (transform.TransformPoint (RightCollisionRay), transform.TransformDirection (RightCollisionRayDir), Hit, 2, layerMask)){
ShipEnergyVar -= Speed;
if (Hit.distance < 0.4){
R_Particle1.emit = true; RS_Particle1.emit = true;
}
if (Hit.distance > 0.6 && Hit.distance < 1.2){
R_Particle2.emit = true; RS_Particle2.emit = true;
}
if (Hit.distance > 1.2){
R_Particle3.emit = true; RS_Particle3.emit = true;
}
}else {R_Particle1.emit = false; RS_Particle1.emit = false;R_Particle2.emit = false; RS_Particle2.emit = false;R_Particle3.emit = false; RS_Particle3.emit = false;}
if (ShipEnergyVar < 0){
Instantiate (Destroyer, transform.position, transform.rotation);
Destroy (gameObject);
}
}
$$anonymous$$ight help if you said what the code was suppposed to do? The rayCasts makes it look like you have 12 warning lights that flash when you get near an obstacle (but not when you are 0.4 to 0.6 away?)
It's a ship which uses only two raycasts to detect collisions and toggle sparks (sparks are made from those not simulated in space and those that are simulated and left behind) in 6 places.
Answer by save · Jan 22, 2012 at 02:46 PM
They will all be cached when you make them into variables. A GO-array is basically a collection of variables under the same roof so it's easier to iterate through them if you need to.
If your emitters are the same you could equally just use one single and reposition it to the point of impact (or what your script does). Have a look at the emit-function.
Anyhow to make them into an array you just do this:
var particles : ParticleEmitter[];
and reference to them like this:
particles[int].emit = true;
I didn't know that I can have such control over particles, and thx for answer!