x


Randomly instantiating an object on a field of planes.

I have say a series of 2-d planes that represent a brick wall, where each brick is separate 2-d plane. I have a prefab brick that is 3d, that I want to have seemingly 'pop out' of the brick wall and approach the screen. After it moves a certain distance on the z-axis towards the viewer I want it to be destroyed and then instantiate from a random 2d brick on the wall.

How can I get it to instantiate from the position of a random object within the scene?

more ▼

asked Nov 17 '10 at 12:30 AM

Wylie gravatar image

Wylie
54 11 12 20

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

4 answers: sort voted first

GameObject myObj = Instantiate(myPrefeb, plane.transform.position, transform.rotation);

or you can set it after instantiating like this:

GameObject myObj = Instantiate(myPrefeb); myObj.transform.position = plane.transform.position;

more ▼

answered Nov 17 '10 at 01:35 AM

denewbie gravatar image

denewbie
717 3 3 17

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

Assuming the bricks were all tagged with a tag called "Brick", you can do this:

var bricks = GameObject.FindGameObjectsWithTag("Brick");
var randomPosition = bricks[Random.Range(0, bricks.Length)].transform.position;

Note that if the number of bricks doesn't change, you should do FindGameObjectsWithTag once and keep the results, rather than every time you want to get a new random position.

more ▼

answered Nov 17 '10 at 01:47 AM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 518

Don't do drugs, kids.

Apr 22 '11 at 10:32 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

i Totally Agree With Yuh Juana !! THis SHit Is Fucking Stuppid !!!! >:O

more ▼

answered Apr 22 '11 at 08:38 PM

Pancha gravatar image

Pancha
1 2

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

A brick is an object used to build things dumb-asses not a airplane!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! get a fukin life nd stop trying to search for bricks peace out bitchesxDDDD

more ▼

answered Apr 22 '11 at 08:23 PM

juana gravatar image

juana
-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:

x1667
x570

asked: Nov 17 '10 at 12:30 AM

Seen: 1257 times

Last Updated: Nov 17 '10 at 12:30 AM