I AM UNABLE TO DESTROY THE CLONE OF THE GAMEOBJECT I AM INSTANTIATING

I AM CREATING A NUMBER OF CLONES OF THE GAMEOBJECT FOR MY GAME.THE MOTTO OF MY GAME IS TO GENERATE A NUMBER OF THE GAMEOBJECT (CLONE) AND I AM SUCCESSFULLY DOING THIS BUT WHEN I AM TRYING TO DESTROY THE CLONE OF THE GAMEOBJECT ON MOUSE CLICK, MY BASIC GAMEOBJECT IS BEING DESTROYED BUT ORIGINALLY ACCORDING TO ME THE CLONES SHOULD HAVE DESTROYED

public class Ball : MonoBehaviour {

public  gamecontroller _gamecontroller;
// Use this for initialization
void Start () {
_gamecontroller = GameObject.FindObjectOfType(typeof(gamecontroller))	as gamecontroller;
}

// Update is called once per frame
void Update () {

}
void OnMouseDown() {
	Destroy(transform.gameObject);
}

}

AND THIS CODE HAS BEEN ATTACHED TO THE GAMEOBJECT I M TRYING TO CLONE AND DESTROY

Please don't SHOUT.

sorry if u r hurt but can u please tell me which word of mine seems u to be shouting I will edit it

I'm not hurt :) But don't use ALL CAPS... it draws unnecessary attention to your post and makes the text hard to read.

thanks for the suggestion I will keep this in my mind now onwards

2 Answers

2

See this example

public Transform shere;
public Transform cube;
private GameObject last;

Vector3 pos = new Vector3(0.0f,12.0f,0.0f);

// Update is called once per frame
void Update () {
    if(Input.GetKeyDown("1") )
       last = Instantiate(shere.gameObject , pos + Random.insideUnitSphere, Random.rotation);
    if(Input.GetKeyDown("2"))
        last = Instantiate(cube.gameObject , pos + Random.insideUnitSphere, Random.rotation);
    if(Input.GetKeyDown("3"))
        if(last)Destroy(last);
}

what if I am instantiating the gameObject in gameController and not in the class I am destroying the object

I found some help ages ago for a script to destroy cloned objects and I’m happy to share it with you as I found it can get a little frustrating.

Basically, attach this script to the prefab object and you can set how long it takes before the object is destroyed in the inspector.

#pragma strict

var timeToEnd : float;

function Update () 

{
        Destroy(gameObject, timeToEnd);
}

The beauty of this is that you can basically write the parameters you want to destroy the object in, I used this to destroy a laser bolt when it reached the edge of the screen.

#pragma strict

var timeToEnd : float;

function Update () 

{
	if (transform.position.x < -8.5f)
	{
		Destroy(gameObject);
	}
	else
	{
		Destroy(gameObject, timeToEnd);
	}
}

Hope it helps!