- Home /
Array Help
Hey guys, I'm trying to clean up my scripts and convert this one in particular into an array. I've never used Arrays before and the Script Reference kind of confused me, so I'm asking if someone would be willing to convert this to an array and explain it to me. Basically, the script enables a HitSpark (small sprite sequence for when something's hit) and goes through each sprite, turning off once the sequence is done. I essentially want to be able to just drag this script onto any Enemy and be able to input the values and add the respective number of sprites, but currently I'm forced to make a new script for each sequence depending on their total number of sprites.
var HS1 : Material;
var HS2 : Material;
var HS3 : Material;
var HS4 : Material;
var HS5 : Material;
var Speed = 0.1;
var HitSpark1 : Transform;
var HitSpark2 : Transform;
var started:boolean = false;
var showing:boolean = false;
function Start(){
HitSpark1.renderer.material = HS1;
HitSpark2.renderer.material = HS1;
yield WaitForSeconds(Speed);
HitSpark1.renderer.material = HS2;
HitSpark2.renderer.material = HS2;
yield WaitForSeconds(Speed);
HitSpark1.renderer.material = HS3;
HitSpark2.renderer.material = HS3;
yield WaitForSeconds(Speed);
HitSpark1.renderer.material = HS4;
HitSpark2.renderer.material = HS4;
yield WaitForSeconds(Speed);
HitSpark1.renderer.material = HS5;
HitSpark2.renderer.material = HS5;
yield WaitForSeconds(Speed);
showing = false;
started = false;
}
function Update ()
{
if(started){
showing = true;
started = false;
if(!started){
Start();
}
}
if(showing){
HitSpark1.renderer.enabled = true;
HitSpark2.renderer.enabled = true;
}else{
HitSpark1.renderer.enabled = false;
HitSpark2.renderer.enabled = false;
}
}
Answer by DaveA · Aug 14, 2011 at 06:41 PM
Something like
var HS : Material[];
var HitSpark : Transform[];
for (var i = 0; i < HS.length; i++)
{
for (var j = 0; j < HitSpark.length; j++)
HitSpark[j].renderer.material = HS[i];
yield WaitForSeconds(Speed);
}
Answer by $$anonymous$$ · Aug 14, 2011 at 06:47 PM
var HS : Material[];
vat HitSpark: Transform[];
drag the prefabs in the inspector.
HitSpark[0].renderer.enabled = true;
you can create a int variable to represents this index.
private var indx : int = 0;
indx = 5;
HitSpark[indx].renderer.enabled = true;
Your answer
![](https://koobas.hobune.stream/wayback/20220612180540im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Array out of range 1 Answer
Remove object from List after gameObject was destroyed in between Scene loads 1 Answer
How to cycle through targets? 2 Answers
How to arrange list 3 Answers