- Home /
Question by
compression · Nov 01, 2011 at 05:23 PM ·
instantiateiphonetouchdestroy
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!
Comment
Answer by FLASHDENMARK · Nov 01, 2011 at 05:33 PM
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);
}
}
}
}
}