spawn random objects in multiple position

hi all.
i am making a puzzle game. and there are 4 position in the game that the pipe appear randomly at first. And when the pipe got dragged out, i want to make a new pipe instantiate at that position. :slight_smile:

i have done the code for the pipes instantiating random at first, click and drag object. but i don’t know how to make it appear randomly again at the position after the pipe got dragged out :(. HELP!!

Here is the full script. Thank :slight_smile:

var blockTypes: GameObject[];
var inGamePos: Array = new Vector3[4];
var pos1: GameObject;
var pos2: GameObject;
var pos3: GameObject;
var pos4: GameObject;
static var clickedObj : Transform; 
private var offSet : Vector3;     
var cubewall : GameObject;
private var snapAllowance: int = 3;
var blockTypesWeight= [1,1,3,3,1,1];

function Start () {

    inGamePos[0] = pos1.transform.position;
    inGamePos[1] = pos2.transform.position;
    inGamePos[2] = pos3.transform.position;
    inGamePos[3] = pos4.transform.position;

    for (i = inGamePos.length - 1; i >= 0; i--) {
       var ranPipe: GameObject = blockTypes[Random.Range(0,blockTypes.length)];
       var newPipe: GameObject = Instantiate(ranPipe,inGamePos*,Quaternion.identity);* = “Block”;

function Update () {
var ray = camera.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) {
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
if(hit.transform.gameObject.tag == “Pipe”) {
clickedObj = hit.transform;
offSet = clickedObj.position-ray.origin;

else if (Input.GetMouseButtonUp(0)) {

if(clickedObj != null&& clickedObj.GetComponent(BlockScript).hitObj != null){
if(clickedObj.gameObject.transform.position.x>=clickedObj.GetComponent(BlockScript).hitObj.transform.position.x-snapAllowance && clickedObj.gameObject.transform.position.x<=clickedObj.GetComponent(BlockScript).hitObj.transform.position.x+snapAllowance)

clickedObj = null;


if (clickedObj) {
clickedObj.position = Vector3(ray.origin.x+offSet.x, ray.origin.y+offSet.y,clickedObj.position.z);



function snap (objToSnap: GameObject, posToSnap: Vector3) {

print(“snap object”);
objToSnap.transform.position.x = posToSnap.x;
objToSnap.transform.position.y = posToSnap.y;
clickedObj.GetComponent(BlockScript).hitObj = null;


function Spawn (// i think i need to put something here ??){

var ranPipe: GameObject = blockTypes[Random.Range(0,blockTypes.length)];
var newPipe: GameObject = Instantiate(ranPipe,inGamePos[0],Quaternion.identity);


Simplest way with the code you have now would be to add a private variable, drugFromPosition : vector3, which you set to the position of clickedObj right after you assign clickedObj to the hit object. Then, in Spawn, instead of a position from inGamePos, you can simply instantiate a new pipe at drugFromPosition.

That’s it for directly solving your problem, but I would like to add, there are many things that could improve the structure of this code. I won’t go into it completely, just point out one big one. A lot of this code could be moved into a script that is attached to the pipes themselves (it seems to be on some sort of game manager script now). If restructured this way, you could eliminate a lot of your code in Update, just using the OnMouseDown and OnMouseUp events of the pipe script instead. The system would then handle the collision testing for you, and you could focus on the desired behavior and eliminate a lot of code!

There’s a bit more to the restructuring than that, but ultimately you could cut the amount of code necessary to achieve the same results in half - or more - by taking full advantage of the things Unity can do for you, instead of doing it all yourself!

Hope this helps!

it worked. thank you very much! but there is problem that it not only spawn in 4 position but also spawn when in cubewall. i mean when i dragged from inGamePos to the cubewall and then click that pipe and drag it again, it instantiate in the cubewall. should i make the pipe non-clickable after place it in the cubewall? how??

and how should i make the code that the new pipe can replace the old pipe in cubewall? :frowning: