Destroy Object on Collision

Hi There,

I have a question on the GameObject.Destroy (NameOfObject); code.

I’ve been trying to remove the rocket at impact (or transform it to an animation at later stage). However, when doing the following tweaks to Ben’s Code it will not do anything (see commented sections for readability guidance):

Any suggestion how to destroy the object?

EDIT

Destroy(gameObject); is the code required to destroy the object. However it now stops the game immediately.

EDIT EDIT

GameObject.Find(“Rocket”).transform.localScale = new Vector3(0, 0, 0);

That one makes it as intended xD

public class FlyingInput : MonoBehaviour
{
    Rigidbody rigidBody;
    AudioSource audioSource;
    [SerializeField] float rcsThrust = 100F; 
    [SerializeField] float mainThrust = 100F;
    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip GameOver;
    [SerializeField] AudioClip IdleThrust;
    [SerializeField] AudioClip Success;
    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem successParticles;
    [SerializeField] ParticleSystem deathParticles;
    KeyCode SpaceKey;
    GameObject Rocket; **// Calling out that the Rocket is here**


    int sceneNumber;
    enum State { Alive, Dying, Transcending, Idling }
    State state = State.Alive;



    public void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    }


    public void OnCollisionEnter(Collision collision)

    {
       sceneNumber = SceneManager.GetActiveScene().buildIndex;
             if (state != State.Alive) { return; }

        switch (collision.gameObject.tag)
        {


            case "Friendly":
                print("Friend");

                break;

            case "Final":
                state = State.Transcending;

                if (sceneNumber == 1)

                {
                    Invoke("LoadCurrentScene", 1f);
                }

                else if (sceneNumber == 0)
                {
                    Invoke("LoadNextScene", 1f);
                }

                break;

            default:

                state = State.Dying;
                if (sceneNumber == 0)
                {
                    deathParticles.Play();
                    GameObject.Destroy(Rocket); // Immediately after state is triggered and death particle activated, the object should be destroyed. 
                    audioSource.Stop();
                    audioSource.PlayOneShot(GameOver);


                    Invoke("LoadCurrentScene", 4f);

                }

                else if (sceneNumber == 1) {
                    deathParticles.Play();
                    GameObject.Destroy(Rocket);
                    audioSource.Stop();
                    audioSource.PlayOneShot(GameOver);

                    { Invoke("LoadNextScene", 4f); }
                }
                break;
        }
    }

The Rocket is named in the inspector

1 Like

Hi!

Your Rocket variable is actually being destroyed, but it is empty, so it won’t do anything, you have to add a value to the Rocket variable, or write this:

Destroy(gameObject);

That will destroy the gameObject the script is attached too, this will also destroy the Childs too, that means that the particles won’t play and sound won’t play either.

One way to remove the Rocket without destroying everything is by doing something like this:

[SerializeField] GameObject Rocket = null; 
/*Setting it to null just prevents 
some warning from showing up*/

Then drag the Rocket main object (The object that holds all of the basic shapes) into the variable, so it only destroys the meshes instead of everything.

1 Like

Thanks Yee;

Yeah I figured out :). The only problem is that if using this the other conditions will trigger the game to restart before the animation can even take place. So I have added this instead of destroy (as the screenload code is priority)

GameObject.Find(“Rocket”).transform.localScale = new Vector3(0, 0, 0);

I am sure if I would tweak all these screenloads a bit so that the status of the gameObject isnt triggering the restart (based on Ben’s shown code). I will keep this one in if you still want to use original code but add that additional flavor of not seeing the rocket anymore after the explosion effect.

Thank you

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms