x


Can't seem to destroy instantiated objects.

Hi all,

I encounter some problems with my destroying instantiated objects. I was able to spawn the objects out, but when it comes to destroying those objects, the codes cannot be worked properly.

var object : GameObject; //for button click to instantiate the objects
var object2 : GameObject;  // object that is instantiated
var back : GameObject;  // button that is instantiated, which is to destroy the instantiated objects.
function Update() {
    if (Input.touchCount > 0) {
       var touch = Input.touches[0];
       var ray = Camera.main.ScreenPointToRay(touch.position);
       var hit : RaycastHit;

       if (Input.GetTouch(0).phase == TouchPhase.Began) {
         if (Physics.Raycast (ray,hit)) {
          if (hit.collider.gameObject == object) {
              Debug.Log("touch ok");
              Instantiate(object2);
              //object2.transform.rotation.x = 270;
              object2.transform.position = Vector3(5.552337, 2.498129, -6.875283);

              Instantiate(back);
              back.transform.position = Vector3(4.303949, 1.765787, -6.884039);
          }
          if (hit.collider.gameObject == back) {
              Debug.Log("touch ok");
              Destroy(object2);
              Destroy(back);
          }
         }
       }
    }
}

Thanks in advance!

more ▼

asked Nov 01 '11 at 05:23 PM

compression gravatar image

compression
16 5 5 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You need to declare a new variable referring to the Instantiated object and then destroy the variable:

var object : GameObject; //for button click to instantiate the objects
var object2 : GameObject;  // object that is instantiated
var back : GameObject;  // button that is instantiated, which is to destroy the instantiated objects.
function Update() {
    if (Input.touchCount > 0) {
       var touch = Input.touches[0];
       var ray = Camera.main.ScreenPointToRay(touch.position);
       var hit : RaycastHit;

       if (Input.GetTouch(0).phase == TouchPhase.Began) {
         if (Physics.Raycast (ray,hit)) {
          if (hit.collider.gameObject == object) {
              Debug.Log("touch ok");
              var newObject2 = Instantiate(object2);
              //object2.transform.rotation.x = 270;
              object2.transform.position = Vector3(5.552337, 2.498129, -6.875283);

              var newBack = Instantiate(back);
              back.transform.position = Vector3(4.303949, 1.765787, -6.884039);
          }
          if (hit.collider.gameObject == back) {
              Debug.Log("touch ok");
              Destroy(newObject2);
              Destroy(newBack);
          }
         }
       }
    }
}
more ▼

answered Nov 01 '11 at 05:33 PM

OrangeLightning gravatar image

OrangeLightning
5.3k 47 57 111

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1998
x1667
x763
x576

asked: Nov 01 '11 at 05:23 PM

Seen: 1231 times

Last Updated: Nov 01 '11 at 05:33 PM