- Home /
Can i use objects from a builtin Array to populate a Js Array
I very little scripting and/or programming experiuence, but i am a quick study. This is my first real work with array, so i dont fully understand the concepts yet, but im getting there.
using Unity Script
What i am trying to do is dynamically create an array from objects stored in a built in array. I want the the new array to add objects that meet certain conditions. From this array, one of the objects will be 'randomly' selected for instantiation.
example:
-Built in array ARR contains objects A, B and C.
-each object has property X.
-Build array New by adding ojects from ARR with an X value above 10.
-get resulting size of array New, and use that as the max value in a random range
Is this possible? Is it the right way to acheive my end result?
thanks
Answer by DaveA · Aug 01, 2012 at 12:07 AM
Did you read the manual? http://docs.unity3d.com/Documentation/ScriptReference/Array.html Let us know if that doesn't answer your question.
in short, it does not answer my question in a way i understand. :)
yep. i read all of that, But i dont understand it yet. not even sure if it says this is or is not possible... When i said very little experience, i meant little to no experiences. I am an artist trying to learn some basic scripting to prototype some ideas.
This may just simply be beyond my current level of mismatched understanding. jumped ahead too far...maybe time to step back to basics. :)
Yeah, those examples on that page are pretty close to what you need. $$anonymous$$ore clues here: http://docs.unity3d.com/Documentation/ScriptReference/Array.Array.html
Untested:
// Creates an empty array var newArr = new Array ();
//Now loop through original array, adding only what you need for (var o in ARR) { if (o.X > 10) // Add element to new array newarr.Push (o); } // Copy the js array into a builtin array var NEW : GameObject[] = newarr.ToBuiltin(GameObject);
Awesome. While you above example was not the whole answer, it provided enough to steer me in the right direction. After scouring the ref manual, and numerous other Answer pages, i think i finally came up with a working example. I have a little more testing to do to make sure. Once i get it all working, i'll post the result for your viewing pleasure (or displeasure due to my amature code!).
Answer by Tsailen · Aug 02, 2012 at 06:34 AM
After a bit of trial and error, i came up with something that worked. This is the pertinent part of the script afer cleanup. I have tested it and it does what i want. Forgive the amature code. If anyone sees a better way to do any of this, or see something i missed, let me know.
var objArr : GameObject[];
var lvlProg = 0;
var maxVal : int;
var instAr = new Array();
function Awake() {
lvlProg = GameStart.lvlProg;
var objTbl = new Array();
for (var obj : GameObject in objArr){
var oWeight = obj.GetComponent(ObPrp);
if (oWeight.weight > lvlProg){
objTbl.Push(obj); } } }
function Start(){
instAr = objTbl;
maxVal = instAr.length;
Generate();
}
function Generate(){
var min = 0;
var max = (maxVal);
var objGen = Random.Range(min,max);
Instantiate (instAr[objGen], transform.position,transform.rotation);
GameStart.lvlProg ++;
Destroy(gameObject);
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Can't edit built-in array through script unless viewed in Inspector 1 Answer
Set variable/Array to a length by scripting 2 Answers
Array of custom class objects all return the same value? 1 Answer