Search Unity

"TheSlayer" tutorials

Discussion in 'Community Learning & Teaching' started by RoyS, Mar 15, 2015.

  1. RoyS

    RoyS

    Joined:
    Jan 12, 2009
    Posts:
    664
    I've been going thru The Slayer's excellent tutorial series on YouTube.


    I have a question on tutorial 6.

    I've got this javascript code
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. //attach this script to the enemy
    5.  
    6. function OnCollisionStay (col : Collision)
    7.      {
    8.           if (col.gameObject.name == "fireball(Clone)") // "clone" shows it's a prefab
    9.           //if you shoot the fireball in-game, hit pause and click on it the name of it is fireball(clone)
    10.           {
    11.                  Destroy(col.gameObject); //destroys the fireball
    12.                  Destroy(gameObject);  //destroys the enemy (the gameObject the script is attached to
    13.           }
    14. }
    15.  
    When I shoot at the enemy, the fireball bounces off of the enemy (doesn't destroy) and the enemy's physics (enemy is a cylinder with collider and rigidbody) takes over and rolls it down the hill. So neither the enemy or fireball is being destroyed. I've looked at the docs and Destroy is still valid. http://docs.unity3d.com/412/Documentation/ScriptReference/Object.Destroy.html
     
  2. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    I have not seen video though looking at your question I will suggest to add "EmptyGameObject" as child to fireball with collider mark it as "isTrigger" to true and then add following code

    1. function OnColldierEnter (col : Collider)
    2. {
    3. if (col.gameObject.name == "fireball(Clone)") // "clone" shows it's a prefab
    4. //if you shoot the fireball in-game, hit pause and click on it the name of it is fireball(clone)
    5. {
    6. Destroy(col.gameObject); //destroys the fireball
    7. Destroy(gameObject); //destroys the enemy (the gameObject the script is attached to
    8. }
    9. }
    Approach two will be
    Just make fireball collider "isTrigger" to true and add above code, that should do.