spawn object

hi i’m making a puzzle game . There are four positions that contain objects. And i need to make an object instantiate again in position that got the pipe dragged out from.

 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;
	Spawn();
	
	}


function Spawn (//what i need to put in here??){
	
		var ranPipe: GameObject = blockTypes[Random.Range(0,blockTypes.length)];
		var newPipe: GameObject = Instantiate(ranPipe,inGamePos[0],Quaternion.identity);

}

hi. there is 4 position in the game that the pipe appear randomly at first. And when the pipe got dragged out, the new pipe instantiate at that position. :slight_smile:

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);*
  • newPipe.name = “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)*
    
  •  	snap(clickedObj.gameObject,clickedObj.GetComponent(BlockScript).hitObj.transform.position);*
    
  •  	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;*

  • Spawn();*

  • }*

function Spawn (){

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

}