Help Destroying clones??

 if(col.gameObject.name == "Target"){
	
    Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
	
	Destroy(gameObject.Find("Target"), 0);

at the moment it instantiates target as a prefab however re-labels it Target(Clone)

this means that I Destroy(gameObject.find(“Target”) is irrelevant.

How can I destroy the clone without creating a seperate script?

well, you could make

 if(col.gameObject.name == "Target"){
	
    Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
	
	Destroy(gameObject.Find("Target"), 0);

into

 if(col.gameObject.tag == "Target"){
	
    var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
    target.tag = "Target";
	
	Destroy(gameObject.FindWithTag("Target"), 0);

thanks JMstudios i am fairly new to this so all your feedback is appreciated.

basically i can see how your script should work but now “Target” none of the attributes in the below script are doing what they are supposed to… any suggestions?

function OnCollisionEnter(col : Collision){

	
	if(col.gameObject.tag == "Target"){
	
    var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
    target.tag = "Target";
	
	Destroy(gameObject.FindWithTag("Target"), 0);
	
	rigidbody.AddForce(Vector3(0,power,0));
	
	Debug.Log("Nice!");
	
	score += 1;
	
  }
  
}

could you give me all of your script so i can test it?

private var currentPos : Vector3;
var prefab : GameObject;
var power : float = 500.0;
var score : int = 0;
 
function Update(){
 
  currentPos = transform.position;
 
}
 
function OnCollisionEnter(col : Collision){

	
	if(col.gameObject.tag == "Target"){
	
    var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
    target.tag = "Target";
	
	Destroy(gameObject.FindWithTag("Target"), 0);
	
	rigidbody.AddForce(Vector3(0,power,0));
	
	Debug.Log("Nice!");
	
	score += 1;
	
  }
  
}

function OnGUI () {
	GUI.Label( Rect(Screen.width - 100, Screen.height - 20, 100, 20), "Counter is: " + (score));
}

What is happening? Is neither object getting destroyed?

and what exactly is this supposed to do? can you explain exactly?

This ^^

Also, I have something for you

Destroy(gameObject.FindWithTag("Target"), 0);

change this to:

Destroy(GameObject.FindWithTag("Target"));

GameObject should be capitalized, as you are working with the type, and you do not need ‘0’ because that is the default value.

what is the script supposed to do matty7?

Why? Isn’t it obvious? It creates an object then instantly deletes it!

why?

im having a hard time figuring this out because i dont know the purpose of the script

Yeah, without context it’s hard to tell the purpose of it.

Is the prefab an explosion or something like that?

What is this script attached to?

well, this seems to work

private var currentPos : Vector3;
var prefab : GameObject;
var power : float = 500.0;
var score : int = 0;
 
function Update(){
 
  currentPos = transform.position;
 
}
 
function OnCollisionEnter (col : Collision){

	
	if(col.gameObject.tag == "Target"){
	
    var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
    target.tag = "Target";
	
	Destroy(GameObject.FindWithTag("Target"));
	
	rigidbody.AddForce(Vector3(0,power,0));
	
	Debug.Log("Nice!");
	
	score += 1;
	
  }
  
}

function OnGUI () {
	GUI.Box( Rect(Screen.width - 100, Screen.height - 20, 100, 20), "Counter is: " + (score));
}

Am I correct in saying that it deletes both the objects?

what do you mean by both objects?

Both the object that collides, and the object that is made through the Instantiate call.

this does:

private var currentPos : Vector3;
var prefab : GameObject;
var power : float = 500.0;
var score : int = 0;
 
function Update(){
 
  currentPos = transform.position;
 
}
 
function OnCollisionEnter (col : Collision){

	
	if(col.gameObject.tag == "Target"){
	
    var target = Instantiate(prefab, Vector3(currentPos.x, currentPos.y+10, currentPos.z), transform.rotation);
	
	Destroy(target, 5);
	Destroy(col.gameObject);
	
	Debug.Log("Nice!");
	
	rigidbody.AddForce(Vector3(0,power,0));
	
	Debug.Log("Nice!");
	
	score += 1;
	
  }
  
}

function OnGUI () {
	GUI.Box( Rect(Screen.width - 100, Screen.height - 20, 100, 20), "Counter is: " + (score));
}

When you have two objects with the same tag, and you call Destroy on the tag, does it only destroy the first it finds, or all of them?

Because:

if(col.gameObject.tag == "Target") // col's tag is "Target"
...
...
var target = Instantiate(...)
target.tag = "Target";  // Target's tag is "Target"

Destroy(GameObject.FindWithTag("Target"));

They both have the same tag, on the call to Destroy, shouldn’t it destroy both?

actually, it would only destroy one of them. and i updated the script, look above