Can't destroy object

Hello, newbie here.
I’m trying to develop a hyper-casual game, pretty simple, where two spheres rotate around the axis-Z, trying to collect good objects and avoid the bad ones.

I couldn’t make both of the circles rotate around as one, so I created a ‘cube’, stretched it and disabled the mesh renderer in it, however, it works, please see the screenshots.

However, when one of the spheres collides with a ‘bad’ object, I want both to be destroyed, and an explosion particle from both spheres.

The problem is, I couldn’t make both of them destroy AND play the particle animation at the same time. I believe its a problem with them being two different objects as player. So, I’ve tried tagging them “Player” and destroy the objects with the tag “Player” in OnCollisionEnter, but this time, I can see the animation, but only one of them gets destroyed(only the one collides with a bad object). My code is below, so is the video.

gi6ram

using System;
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using TMPro;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{ 
    public float rotationSpeed = 50f; // Speed of rotation
    private bool clockwise = true; // Flag to track rotation direction
    public AudioClip tapSound;
    public AudioClip crashSound;
    public AudioClip pointSound;
    public ScoreManager scoreManager;
    public ParticleSystem explosionParticle;
    public ParticleSystem explosionParticle2;
    private AudioSource playerAudio;


  
    // Start is called before the first frame update
    void Start()
    {
        playerAudio = GetComponent<AudioSource>();
        RotateCube();
    }

    // Update is called once per frame
    void Update()
    {
        // Check for mouse click
        if (Input.GetMouseButtonDown(0))
        {
            playerAudio.PlayOneShot(tapSound, 1f);
            // Change rotation direction
            clockwise = !clockwise;

            // Rotate the cube
            RotateCube();
        }
    }
    void RotateCube()
    {
        // Determine rotation direction based on the flag
        float rotationDirection = clockwise ? 1f : -1f;

        // Apply rotation to the cube around its Z-axis
        GetComponent<Rigidbody>().angularVelocity = Vector3.forward * rotationSpeed * rotationDirection;
    }
  
      public void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Bad"))
        {
            Destroy(other.gameObject);
            explosionParticle.Play();
            explosionParticle2.Play();
            playerAudio.PlayOneShot(crashSound, 1f);
            Debug.Log("Game Over!");
         
        }
      

        else if (other.gameObject.CompareTag("Good"))
        {
            playerAudio.PlayOneShot(pointSound, 1f);
            Destroy(other.gameObject);
            scoreManager.UpdateScore();
          
            }
        }
    }

As far as I can tell you’re not destroying either object in OnCollisionEnter: you’re just destroy the object they collide against, and playing the particle explosion for both.

Note that the particle system components must be on separate objects, otherwise when you destroy the circles you also destroy their particle systems.

Exactly, I haven’t written any code there (the ones I’ve tried didn’t work, so I deleted them upon uploading here)

I’m aware, I want to end the game when I collide with that certain object, so, I should be fine?

So how are we supposed to help with these codes that don’t work, if you don’t post them? :face_with_spiral_eyes:

No one can really answer this question but you… what happens when your game ends? does it stay on the same scene or do you move to a new one? if you’re staying on the same scene, is it ok for you if the circles are kept around or would you like them to be destroyed/go away?

That’s a TON of different things all in play. Nobody should subject themselves to that level of crazy complexity while learning a new context / environment.

Here is a much better strategy: do things one tiny step at a time and don’t assume anything: look in the documentation as you go along and learn it piece by piece, layer by layer.

Imphenzia: How Did I Learn To Make Games:

Thank you for your answer :slight_smile: Basically, the game will be it. There will be a restart button and that’s it. I just want both spheres to be destroyed, when collided with a black cube.

You’re right, I’m sorry. There was a line under the OnCollisionEnter class.
Destroy(GameObject.FindGameObjectWithTag(“Player”));

Great video, thank you! You’re right. I actually thought I’m moving step by step.

Great inspiration, thank you!