1
votes

I have made a game where I spawn in several circles which shrink over time until they are supposed to vanish. The problem is, I make all of the circles with the instantiation function. This creates "Ball(clone)" and whenever I try to use Destroy(GameObject) to get rid of one of them i get the following error.

Can't destroy Transform component of 'Ball(Clone)'. If you want to destroy the game object, please call 'Destroy' on the game object instead. Destroying the transform component is not allowed.

To be clear, the creation of the balls is handled by one script attached to an empty child, and the destruction is another script attached to the ball. They are as follows.

var Xpos : float;    
var Ypos : float;    
var Ball : Transform;

//Place ball
function Update ()  
{    
    if (Input.GetMouseButtonDown(0))  
{  
  //debugging
  Xpos = Input.mousePosition.x;
  Ypos = Input.mousePosition.y;

  //Get mouse input and convert screen position to Unity World position
  var position : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  Instantiate(Ball,Vector3(position.x,position.y,1),Quaternion.identity); 
}
}

//delete ball

#pragma strict

var Ball : Transform;

function Update () 
{
    Ball.animation.Play("Shrink");
}


function Despawn ()
{
    Destroy(Ball);
}
1

1 Answers

1
votes

The error message says it all; you can't Destroy() a Transform. You'll have to apply that to the GameObject instead.

Changing to Destroy(Ball.gameObject); should achieve just that.