0
\$\begingroup\$

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,
                float PaddleRight)
{
    // 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?
    if (col.gameObject.name == "PaddleLeft")
    {
        // 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?
    if (col.gameObject.name == "PaddleRight")
    {
        // 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();
    }

    if (col.gameObject.name == "WandLinks")
    {

    }
}
}
\$\endgroup\$
  • \$\begingroup\$ What is your Dot Gameobject in the game? Get the reference to that on Destroy it. \$\endgroup\$ – Skalli Jan 8 at 15:33
  • \$\begingroup\$ 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. \$\endgroup\$ – Redgrieve Jan 8 at 15:37
  • \$\begingroup\$ 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);. \$\endgroup\$ – MichaelHouse Jan 8 at 15:54
  • \$\begingroup\$ 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 :/ \$\endgroup\$ – Redgrieve Jan 8 at 16:05
  • \$\begingroup\$ It likely doesn't move because the script isn't present on the new copy. Why this is we can't determine through comments. \$\endgroup\$ – Draco18s Jan 8 at 18:46
1
\$\begingroup\$

This what I use to destroy my objects: GameObject.Destroy(gameObject);

\$\endgroup\$
  • \$\begingroup\$ Thank you for help MrDrProfessor :) But when I try this it tells me "Dot" is a class and can't be destroyed. How do I turn my class into a gameObject? \$\endgroup\$ – Redgrieve Jan 8 at 15:32
  • \$\begingroup\$ In Unity you can drag your scripts onto the game objects in the scene or use add component in the inspector. Have you tried replacing the line of code "Destroy(Dot.gameObject);" to "GameObject.Destroy(gameObject);"? \$\endgroup\$ – MrDrProfessor Jan 8 at 15:40
  • \$\begingroup\$ Ah! If I drag the script onto the object it basically creates a reference with "gameObject" being the reference? \$\endgroup\$ – Redgrieve Jan 8 at 15:44
  • \$\begingroup\$ @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. \$\endgroup\$ – Draco18s Jan 8 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.