Search Unity

Destroy (gameObject)

Discussion in 'Editor & General Support' started by MikeC, Sep 3, 2006.

  1. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    I now have enemies in the level, yay! :)

    However, it's difficult to get rid of them, lol! I've written a script so when the main character collides with the enemy, it plays the hit animation and that works great.

    However, if the character is playing the "spin" or "jump" animation, I need the enemy to be destroyed. Here's what I have so far:

    Code (csharp):
    1. function OnTriggerEnter (other : Collider) {
    2.   if (other.name == "moto_bug") {
    3.     if (!animation.IsPlaying ("spin"))
    4.     Destroy (other.name == "moto_bug");
    5.     }
    6.     else
    7.     animation.CrossFade("dies");
    8. }
    But it's not happy with the "Destroy..." line at all for some reason. I've tried a whole bunch of different things in those brackets but nothing works!

    Just for clarification, the script is added to the main character and not the enemy.

    Any ideas?

    Mike
     
  2. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Change the 4th line to just say:

    Destroy (other);

    Good luck!
     
  3. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    Not working, I've now got some really weird goings on though!

    Now, for some reason, when I collide with the badnik the hit animation never plays and the badnik doesn't get destroyed whether the spin animation is playing or not.

    Also, the ocean, which I had triggered to play the falling animation, seems to be combining some of my other scripts for some reason. Now, when I hit the ocean, I get the debug message saying I've hit a badnik, the hit animation plays, and the character gets thrown in the air like the spring animation (But for some reason this automatic reaction doesn't work on the spring itself!) This is quite mental, lol!

    Mike
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Destroy (other); will simply remove the collider. Instead you want to delete the whole game object.

    thus

    Destroy (other.gameObject);
     
  5. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    That explains that one! I'm so glad you're around! :)

    Mike
     
  6. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    Nope, still not working right :(

    Here's what I've got now:

    Code (csharp):
    1. function OnTriggerEnter (other : Collider) {
    2.    if (other.name == "moto_bug") {
    3.       if (!animation.IsPlaying ("spin"))
    4.       Destroy (other.gameObject);
    5.       Debug.Log ("Destroyed Badnik");
    6.       }
    7.       else
    8.       animation.CrossFade("dies");
    9.       Debug.Log ("Hit by Badnik");
    10.    }
    For some reason, the badnik isn't destroyed when the spin animation is playing, but it is destroyed if any other animation is playing! I can spin through the thing all I like and not get it but if I just stand still or run through it, it'll be destroyed!

    I can't get my head around it fully.

    Any ideas?

    Mike
     
  7. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Aha! The exclamation point means NOT. So it's saying if you're NOT spinning, kill the badnik, and if you are then it won't kill it. So just remove the "!" on line 3.
     
  8. MikeC

    MikeC

    Joined:
    Jul 15, 2006
    Posts:
    63
    Thank you!!!! :D

    Oh dear, I can't beleive I never thought to get rid of the "!"!!! It works and it's great! I'll have to work on a destroy animation now!

    Right, on this happy note, I'm packing it in for the night and getting ready for bed. Hopefully I'll be able to work on this more next weekend!

    Goodnight all, and thanks for the help the past few days! :)

    Mike
     
  9. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Woo. I actually helped someone! n_n