Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how to destroy objects?

Discussion in 'Scripting' started by mushey, Nov 28, 2016.

  1. mushey

    mushey

    Joined:
    Nov 13, 2016
    Posts:
    39
    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
    Code (CSharp):
    1. void onTriggerEnter(Collider other)
    2.     {
    3.         if (other.tag == "Boundary")
    4.         {
    5.             return;
    6.         }
    7.         Destroy(other.gameObject,0.0f);
    8.         Destroy(gameObject);
    9.        
    10.     }
    11. if you want to quickly check the video its(the 9th video)
    12. https://www.youtube.com/watch?v=OtPSvgr_ywY
     
  2. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    Why did you write 0s on line 7?

    It should be:
    Code (CSharp):
    1. Destroy(other.gameObject);

    Moreover, it is very important to spell functions correctly:
    OnTriggerEnter, but not onTriggerEnter
     
    mushey and JoeStrout like this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    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:

    Code (CSharp):
    1.    Debug.Log("Foo!");
    ...and then make sure that message appears in the Console when you think it should.
     
    romatallinn likes this.
  4. mushey

    mushey

    Joined:
    Nov 13, 2016
    Posts:
    39
    i added a float value because i thought it requires one
    and yh my bad i didnt write the function correctly.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,375
    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.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    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.
     
    Kurt-Dekker likes this.
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Also worth noting that you used the version that doesn't take a float right below the one where you passed 0 :)
     
  8. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,407
    JoeStrout likes this.