- Home /
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?
Answer by denewbie · Nov 17, 2010 at 01:35 AM
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;
Answer by Eric5h5 · Nov 17, 2010 at 01:47 AM
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.
Answer by Pancha · Apr 22, 2011 at 08:38 PM
i Totally Agree With Yuh Juana !! THis SHit Is Fucking Stuppid !!!! >:O
Answer by juana · Apr 22, 2011 at 08:23 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613083329im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
For Loop isn't working properly! 1 Answer
Creating a light in game C# 1 Answer
Random.value sometimes doesn't instantiate my prefab(pic included) 1 Answer
Spawn Random Enemy 3 Answers
instantiating vertically 2 Answers