Create a copy of an gameobject

ssuppal picture ssuppal · Aug 20, 2010 · Viewed 10.8k times · Source

How do you create a copy of an object upon mouse click in Unity3D?

Also, how could I select the object to be cloned during run-time? (mouse selection preferable).

Answer

Vivek2012 picture Vivek2012 · Apr 1, 2011
function Update () {

    var hit : RaycastHit = new RaycastHit();
    var cameraRay : Ray  = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast (cameraRay.origin,cameraRay.direction,hit, 1000)) {
        var cursorOn = true;
    }

    var mouseReleased : boolean = false;

    //BOMB DROPPING 
    if (Input.GetMouseButtonDown(0)) {

        drop = Instantiate(bomb, transform.position, Quaternion.identity);
        drop.transform.position = hit.point;

        Resize();

    }
}

function Resize() {
    if (!Input.GetMouseButtonUp(0)) {
            drop.transform.localScale += Vector3(Time.deltaTime, Time.deltaTime,
                                                 Time.deltaTime);
            timeD +=Time.deltaTime;
     }
}

And you'll want this to happen over course of many calls to Update:

function Update () {
    if(Input.GetMouseButton(0)) {
        // This means the left mouse button is currently down,
        // so we'll augment the scale            
        drop.transform.localScale += Vector3(Time.deltaTime, Time.deltaTime,
                                             Time.deltaTime);
    }
}