how to destroy objects?

hi im going through the space shooter tutorial and its really challenging because things have changed , i need my laser to destroy the asteroid and the asteroid to explode aswell
heres what i have currently but its not working

void onTriggerEnter(Collider other)
    {
        if (other.tag == "Boundary")
        {
            return;
        }
        Destroy(other.gameObject,0.0f);
        Destroy(gameObject);
       
    }
if you want to quickly check the video its(the 9th video)
https://www.youtube.com/watch?v=OtPSvgr_ywY

Why did you write 0s on line 7?

It should be:

Destroy(other.gameObject);

Moreover, it is very important to spell functions correctly:
__O__nTriggerEnter, but not __o__nTriggerEnter

2 Likes

I suspect @romatallinn is right, the problem is that your code is never executing because you’ve capitalized it wrong.

Always begin debugging by seeing if your code is running at all. Just insert a line like this in the suspect method:

   Debug.Log("Foo!");

…and then make sure that message appears in the Console when you think it should.

1 Like

i added a float value because i thought it requires one
and yh my bad i didnt write the function correctly.

I recommend ALWAYS going to the reference Unity scripting website and copy/pasting the capitalization for these functions, things like OnTriggerEnter, etc… it’s WAY too easy to get it wrong. I always do this copy/paste the first time I use a function name, just to make sure the camel-case capitalization is correct.

Or if you happen to be using Script Inspector 3 as your code editor (which I highly recommend), just start typing the name of the magic method (without “void” or “protected” or any such modifiers), and Si3 will see what you’re doing and suggest any matching method names. Then you just hit Return, and it fills in the full signature, with a comment about when/how to use it and, of course, the correct capitalization.

1 Like

Also worth noting that you used the version that doesn’t take a float right below the one where you passed 0 :slight_smile:

There is a thread dedicated to the Space Shooter tutorial: Space Shooter Tutorial Q&A - Learn Content & Certification - Unity Discussions

1 Like