Placing an object with limits

Hi there.

I’ve used the mouse object placement found on the unity coder blog in order to help me instantiate some game objects whenever I want to but I appear to have an issue.

Does anyone know how I could add to the below code something like a limit or bounds around the gameobject that is instantiated? I am asking this because whenever I click on the item that is instantiated it instantiates another one which it allows me to move instead of simply allowing me to move the first one.

Thanks,
Eugen

#pragma strict
 
public var ObjectToPlace:Transform; 
private var clone:Transform;
 
function Update (){
 
if(Input.GetButtonDown("Fire1"))
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
var layerMask = 1 << 8;
 

if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask))
{
clone = hit.transform; 
 
}else{ 
 
layerMask = ~layerMask; 
if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask))
clone = Instantiate(ObjectToPlace, hit.point, Quaternion.identity);
}
}
 
if(Input.GetButtonUp("Fire1")) 
{
clone=null; 
}
 
if(Input.GetButton("Fire1")) 
{
if (clone!=null) 
{
var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit2 : RaycastHit;
var layerMask2 = 1 << 8;
layerMask2 = ~layerMask2;
if (Physics.Raycast (ray2, hit2, Mathf.Infinity, layerMask2)) 
{
clone.position = hit2.point;
}
}
}
}

Hello guys,

It took a while but I got it to work. The issue wasn’t with this code actually but with the fact that I was using the object and instantiating it as it was instead of creating a prefab and instantiating.

It was the simplest solution I could figure out and I added a couple of boolean statements so that I can trigger the placement whenever I want.