- Home /
Having problems with destroying, instantiating
Hey guys, I've been creating a game for a good time now, but I've been having problems with destroying and instantiating (probably). So the game is quite simple(http://prntscr.com/752tsj) , when the right time comes you need to click the white and it will count as score, but if you miss one it will be failed. So the problem I'm having is, the 1st block that falls (not a clone but it's default) and i click it, everything seems to work fine, but after the second block falls, it gets me the error message while trying to click the white box: the object type 'GameObject' has been destroyed but you're still trying to access it. And nothing happens, neither it destroys or adds the score, here's my destroying code:
void OnMouseDown()
{
anim.SetTrigger("Flashed");
Vector3 difference = this.gameObject.transform.position - box.transform.position;
if (difference.magnitude <= 1.6f)
{
Destroy(box);
score++;
}
try Destroy (box.gameObject) Happened to me once in a previous project, if not it please show your instantiate script too
Well, you're destroying "box" not "this". So, where is the "box" variable set?