x


Random.range multiple instantiations without repetition

Hello all, Ive been at this for the last day or so, and its starting to bug me - I have no problem instantiating several random game objects out of an array of multiple game objects, I can grasp the random.range with no problem, but when it comes to making sure the same game objects aren't instantiated more that once, I'm having difficulty, Ive tried a few scripts across the net but am having trouble.. Could anybody give me a hand?

Heres my code:

var bodyParts : GameObject[];

function GenerateBodyParts() {
var bodyPartsNum : int = Random.Range(0, bodyParts.length);
    for (var i = 0; i < 3; i++) {
       Instantiate(bodyParts[bodyPartsNum], transform.position + Vector3(0.05,0.05,-0.9), Quaternion.identity);
    }

}

I'd really appreciate some help guys, in JS if possible as I'm not very competent in any other language at the moment.

I know there are scripts out there but i can't seem to get them to work for this function, i.e. multiple random instantiations.

Cheers - monster.

more ▼

asked Apr 18 '12 at 08:30 PM

ratboy gravatar image

ratboy
41 18 21 22

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

1 answer: sort newest

Tested it and it works. Added a 1 second wait just so you could tell the order was random.

var bodyParts : GameObject[];

function Start()
{
    GenerateBodyParts();
}


function GenerateBodyParts() {
    //make a copy of the original one
    //since we will be removing them from the array when we create them
    var bodyPartsCopy : GameObject[] = bodyParts;

    //loop through each bodypart
    for (var i = 0; i < bodyParts.Length; i++) {
    //pick a random index
    var bodyPartsNum : int = Random.Range(0, bodyPartsCopy.Length-1);
    //instantiate the bodypart
    yield WaitForSeconds(1);
    Instantiate(bodyPartsCopy[bodyPartsNum], transform.position + Vector3(0.05,0.05,-0.9), Quaternion.identity);

    //now need to remove the bodypart from the array so we don't create it again
    //but we need to make it a JS array first, remove it, then convert back to a builtin array to be used by unity
    var bodyPartsJS : Array = bodyPartsCopy;
    bodyPartsJS.RemoveAt(bodyPartsNum);
    bodyPartsCopy = bodyPartsJS;

    }
}
more ▼

answered Apr 18 '12 at 08:41 PM

Lttldude gravatar image

Lttldude
1.2k 1 2 7

That looks like its headed in the right direction, thanks for the quick response - but I'm getting the following errors:

Unknown identifier: 'count'. RemoveAt is not a member of UnityEngine.GameObject[] ToBuiltin is not a member of UnityEngine.GameObject[]

any ideas? thanks again.

Apr 18 '12 at 08:54 PM ratboy

I got rid of the whole count variable. I figured out you didnt need it. Hope it works.

Apr 18 '12 at 09:23 PM Lttldude

yeah of course, its already in the bodyPartsNum.. any ideas to the ToBuiltIn and RemoveAt not being members of UnityEngine.GameObject[] ? cheers!

Apr 18 '12 at 10:05 PM ratboy

Updated my answer, all works.

Apr 18 '12 at 11:37 PM Lttldude

Will test in the morning, and will let you know then - thanks for the quick reply! :)

Apr 19 '12 at 02:03 AM ratboy
(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:

x1725
x598
x264
x59
x47

asked: Apr 18 '12 at 08:30 PM

Seen: 601 times

Last Updated: Apr 19 '12 at 02:49 AM