# How do I destroy a class/how do I link a class to a gameObject

I am fairly new to programming and I decided to create a Pong game. I created the sprites and was able to make the paddles and the dot work. The only help I needed was when I wanted the dot to recognize where it was hit.

Now I wanted to respawn the dot when it hits an wall behind the racket. I created an transparent sprite with a 2D Collider. Now I wanted to use this:

If dot hits LeftWall destroy gameobject dot

But when I use:

 if (col.gameObject.name == "WandLinks")
{
Destroy(Dot.gameObject);
}


it tells me "Dot" is a class and can't be destroyed. This I understand. But why does the rest work? And how do I create a reference from the class to a new gameObject? Thank you very much.

(My next solution is to place the code for the destruction inside my wall but that's not my desired solution)

- This is my whole code for the dot

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Dot : MonoBehaviour

{
public float speed = 5;
public Transform spawnPoint;

void Start()
{
// Initial Velocity
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
}

float hitFactor(Vector2 ballPos, Vector2 racketPos,
{
// ascii art:
// ||  1 <- at the top of the racket
// ||
// ||  0 <- at the middle of the racket
// ||
// || -1 <- at the bottom of the racket
return (ballPos.y - racketPos.y) / PaddleRight;
}
public void RespawnDot()
{
Instantiate(dot, spawnPoint.position, spawnPoint.rotation);
}

void OnCollisionEnter2D(Collision2D col)
{
// Note: 'col' holds the collision information. If the
// Ball collided with a racket, then:
//   col.gameObject is the racket
//   col.transform.position is the racket's position
//   col.collider is the racket's collider

// Hit the left Racket?
{
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);

// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(1, y).normalized;

// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}

// Hit the right Racket?
{
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);

// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(-1, y).normalized;

// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}

if (col.gameObject.name == "WandRechts")
{
RespawnDot();
}

{

}
}
}

• What is your Dot Gameobject in the game? Get the reference to that on Destroy it. – Skalli Jan 8 at 15:33
• That basically is my question :D How do I create a reference to my class or the sprite I am using? Everything I wrote worked fine; I though it created the reference as I placed the script inside my sprite. – Redgrieve Jan 8 at 15:37
• You sure that's all your code? I don't see dot being defined (referenced in RespawnDot()). If you want to destroy the current Dot just call Destroy(this);. – MichaelHouse Jan 8 at 15:54
• Nono, the dot part isn't all mine as I said. Thats where I needed help with. Destroy(this) didn't work. But "Destroy(gameObject). Didnt know that Unity created this reference by itself. Problem now is: public void RespawnDot() { Instantiate(gameObject, spawnPoint.position, spawnPoint.rotation); } When I instantiate the dot it spawns but doesn't move as before. How do I move the object afterwards? Thats the last question - after that the game is complete. Sorry for bothering too much :/ – Redgrieve Jan 8 at 16:05
• It likely doesn't move because the script isn't present on the new copy. Why this is we can't determine through comments. – Draco18s Jan 8 at 18:46

• @Redgrieve Dot is a class... Yes, you need to destroy the instance. And in this instance (pun intended) that instance is this. And as you actually want to destroy the GO that the script is attached to, this answer is correct. – Draco18s Jan 8 at 18:43