- Home /
String Arrays in a function
I'm not sure if I can explain this, but I need a function that does the following. Lets say I have a tilebased map like this:
01 02 03 04 05
06 07 08 09 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
They're each a gameObject called platform1, platform2, platform3 etc... Now I want to let an explosion occur on each tile based on the input I give to the function. This I what I got
function Explode(platform : String) { parentObject = transform.Find(platform); var explosion : Transform; var detector : Transform; parentx = transform.Find(platform).position.x; parenty = transform.Find(platform).position.y; parentz = transform.Find(platform).position.z;
explosion = Instantiate(explosionPrefab, Vector3(parentx,parenty+0.2,parentz), transform.Find(platform).rotation);
explosion.parent = parentObject;
detector = Instantiate(cDetectorPrefab, transform.Find(platform).position, transform.Find(platform).rotation);
detector.parent = parentObject;
yield WaitForSeconds(0.2);
Destroy(gameObject.Find(platform+"/CollisionDetector(Clone)"));
}
But now I have to add lots of code like:
Explode("platform1");Explode("platform2");Explode("platform3");Explode("platform4");
that for 25 tiles and also 10 different combinations, so I need something like:
Explode( 01, 02, 03, 04, 05, 06, 07, x, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 );
Explode( 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, x, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 );
and so on for each combination. This will clean up my code alot and it will make it easier to set each combination. :)
Does anyone know how to do this?
Thanks in Advance, Dave
I'm not exactly sure what you're asking? Are you wanting to know if you can pass an arbitrary number of parameters to a function? If so look up the params keyword. void Explode( params string[] tiles){}
http://unity3d.com/support/documentation/ScriptReference/Array.html read this. And for the explosions you can for instance do for(i=0;i
Or have an array keeping track of which one to do and which ones not to do. Have as many entries in the array as you have platforms. Set them all to true. Set the ones with the same number as the platforms you don't want to explode to false and then use if(arrayName[i]).
When coding for a large group of objects always try to use arrays and for loops ins$$anonymous$$d of doing each manually, that's just a waste of time.
What ever happened to actually answering questions in the Answer box below?
Your answer
Follow this Question
Related Questions
ios cant access variables in array 1 Answer
Array of Array 1 Answer
Issues with string array taken from a .txt file 2 Answers
Converting String Array to Int. 2 Answers
How to make certain elements in an array rarer when using random selection? 4 Answers