Search Unity

Destroying a GameObject that has Sprite Renderer on it.

Discussion in 'Scripting' started by DarkNeo, Mar 9, 2015.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hmm So I am a little confused... I have my Player and a Death Zone so if the player hits the Death Zone he is supposed to die.

    My Player has a Box Collider 2D on it, RigidBody 2D and a Sprite Renderer attached. Also tag and gameobject is called "Player" Kinematic is unchecked.

    My Death Zone has a Box Collider 2D attached with my death script what is below.

    Code (JavaScript):
    1. var isDead = false;
    2. var centerDead : GUIStyle;
    3. var centerPlay : GUIStyle;
    4. var centerScore : GUIStyle;
    5. var centerHScore : GUIStyle;
    6.  
    7.  
    8. function OnGUI()
    9. {
    10.     if (isDead)
    11.  
    12.         {
    13.      
    14.          GUI.Box (Rect(Screen.width/2 - 50, Screen.height/2 + -30, 100, 50), "You're Dead", centerDead);
    15.         //GUI.Label (Rect(Screen.width/2 - 50, Screen.height/2 + 25, 100, 50), "Score : ", centerScore);
    16.         //GUI.Label (Rect(Screen.width/2 - 50, Screen.height/2 + 25, 100, 50), "Highscore : ", centerHScore);
    17.         if(GUI.Button (Rect(Screen.width/2 - 125, Screen.height/2 + -120, 250, 150), "", centerPlay))
    18.         {      
    19.              Application.LoadLevel("Scene");
    20.              isDead = false;
    21.         }
    22.     }
    23. }
    24.  
    25. function OnCollisionEnter2D(Player : Collision2D)
    26. {
    27.     if (Player.gameObject.tag == "DeathZone" )
    28.     {  
    29.         Debug.Log ("Hit");
    30.         Destroy(Player.gameObject);
    31.     }
    32.  
    33.     isDead = true;
    34. }
    This code is from one of my previous games, and the only thing I can put it down too is the Sprite Renderer on my Player GameObject.

    on my Previous game my character had a Mesh Filter and also a Mesh Renderer attached to it.

    I have been searching the net but not sure, Can you destroy a Game Object that has a Sprite Renderer attached?


    Any help would be awesome, Cheers :)

    EDIT: Also I can not get my Debug.Log to work at all, everything is returning fine from my console no errors. So trying to Debug.Log it is not helping either. :(
     
    Last edited: Mar 9, 2015
  2. Mr. Mud

    Mr. Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    My assumption would be that you are checking whether the player has the "DeathZone" tag, rather than "Player" tag. That would explain why that Debug.Log is not triggered. If that does not work, you could try a Debug.Log at the start of OnCollisionEnter2D, where it logs the tag of the object it collided with as that could give some more insight in what is going wrong.
     
    DarkNeo and RiokuTheSlayer like this.