I have this private var which represents an array of barrels on my platform that this script is attached to.
private var BarrelsThisPlatform : Transform[] = new Transform[12];
On my scripts Start() I populate this array with my barrels:
var i=0;
for (var child : Transform in transform) {
if (child.name == "Barrel"){
BarrelsThisPlatform[i] = child;
i++;
}
}
Then in another method I want to blow up a random number of these barrels. My thought process is to:
- Randomly sort the barrels
- Splice the first X elements of my transform array into a new array.
- Loop across this new array and call ‘BlowUp’ on these barrels.
The problem I’m running into is that the Transform[ ] doesn’t have all the methods that a normal array has seen here. OR, maybe it doesn’t and I’m not calling it correctly. Can anyone assist me with this?
Here’s my code:
function openBarrels(){
var numOfBarrels: int = BarrelsThisPlatform.Length -1;
var barrelsDestroyed: int = 0;
while(barrelsDestroyed <= barrelsToDestroy){
var myRandom: int = Random.Range(0,numOfBarrels);
var other : BarrelBehaviors = BarrelsThisPlatform[myRandom].gameObject.GetComponent(typeof(BarrelBehaviors));
other.BlowUp();
barrelsDestroyed++;
}
}