x


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

more ▼

asked Aug 01 '12 at 12:04 AM

Tsailen gravatar image

Tsailen
1 1

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Did you read the manual? http://docs.unity3d.com/Documentation/ScriptReference/Array.html Let us know if that doesn't answer your question.

more ▼

answered Aug 01 '12 at 12:07 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

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. :)

Aug 01 '12 at 12:57 AM Tsailen

Yeah, those examples on that page are pretty close to what you need. More 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);
Aug 01 '12 at 01:24 AM DaveA

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!).

Aug 01 '12 at 09:07 PM Tsailen
(comments are locked)
10|3000 characters needed characters left

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);

}

more ▼

answered Aug 02 '12 at 06:34 AM

Tsailen gravatar image

Tsailen
1 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5089
x3463
x1363

asked: Aug 01 '12 at 12:04 AM

Seen: 226 times

Last Updated: Aug 02 '12 at 06:34 AM