Search Unity

Can't get player to destroy

Discussion in 'Scripting' started by bscarl88, Apr 8, 2015.

  1. bscarl88

    bscarl88

    Joined:
    May 11, 2012
    Posts:
    39
    I'm following the Brackey's 2D platformer tutorial, (his forums won't validate my email address so I can't post there), but my script won't delete my player object when he falls off the screen.

    Here's my player script. Line 15-18 is where it doesn't seem to be doing calling DamagePlayer()
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     [System.Serializable]
    7.     public class PlayerStats {
    8.         public int Health = 100;
    9.     }
    10.  
    11.     public PlayerStats  playerStats = new PlayerStats();
    12.  
    13.     public int fallBoundary = -20;
    14.  
    15.     void update(){
    16.         if (transform.position.y <= fallBoundary)
    17.                         DamagePlayer (9999999);
    18.     }
    19.  
    20.     public void DamagePlayer(int Damage) {
    21.         playerStats.Health -= Damage;
    22.         if (playerStats.Health <= 0) {
    23.             GameMaster.KillPlayer(this);  
    24.         }
    25.     }  
    26.  
    27. }
    Here's the GameMaster Script that kills the player:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameMaster : MonoBehaviour {
    5.  
    6.     public static void KillPlayer (Player player) {
    7.         Destroy (Player.gameObject);
    8.     }
    9.  
    10. }
    11.  
    Using Unity 4.6.1
     
  2. oscaro

    oscaro

    Joined:
    Apr 7, 2015
    Posts:
    18
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  4. bscarl88

    bscarl88

    Joined:
    May 11, 2012
    Posts:
    39
    I put the lower case 'p' but it made no difference.


    I'm pretty new to all of this, and this is my first attempt at scripting (I'm just typing what the video tells me, and learning what each line does) but where would I put that in on the script? within the update part? IT doesn't like the word function anywhere, and when I put it within the update script, it doesn't like the '}' Oh man, I'm pretty lost if I cant even add a damn line of code
     
  5. oscaro

    oscaro

    Joined:
    Apr 7, 2015
    Posts:
    18
    Functions can't go inside other functions, so you can't put OnBecameInvisible() inside Update(). You put OnBecameInvisible() outside of Update() as it's a function just like Update(). The function will only run when the object its attached to goes outside the view of the camera. So put the function in your Player script outside of Update() with
    Code (csharp):
    1.  Destroy(gameObject);
    inside of it
     
  6. bscarl88

    bscarl88

    Joined:
    May 11, 2012
    Posts:
    39
    I tried
    Code (CSharp):
    1.     void OnBecameInvisible () {
    2.         enabled = false;
    3.         Destroy (GameObject);
    4.     }
    and tried

    Code (CSharp):
    1.     void OnBecameInvisible () {
    2.         //enabled = false;
    3.         Destroy (Player.GameObject);
    4.     }
    and a few others, and it gives me errors:

    "Expression denotes a 'type' , where a 'variable', or 'value' or 'method group' was expected."

    The best overload method match for UnityEngine.Object.Destroy(UnityEngine.Object)' has some invalid arguments

    and

    Argument '#1' cannot convert 'object' expression to type 'UnityEngine.Object'

    I'm a frustrated failure.... are there any easier platforming tutorials that I could start with that anyone suggests? I can program, and made a few good games in C2 (I know, visual programming ;P ) but I can't seem to really get all the tiny things that can make the code blow up.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you cant use a class as the parameter...

    Player is a class
    GameObject is a class

    Code (csharp):
    1.  
    2. Player player;
    3.  
    player is the object (which you would destroy), of class Player

    GameObject is the class
    gameObject is the object

    Pay attention to the casing used, as its important.

    Destroy(gameObject) will work.
    Destroy(player.gameObject) should also work.

    The following will never work as they are referencing classes rather than the objects themselves.
    Destroy(GameObject)
    Destory(Player.GameObject)
    Destrory(Player.gameObject)
    Destroy(player.GameObject)
     
  8. oscaro

    oscaro

    Joined:
    Apr 7, 2015
    Posts:
    18
    Yeah James is correct. You can't use GameObject, only gameObject will work whichever way you use it.
     
  9. bscarl88

    bscarl88

    Joined:
    May 11, 2012
    Posts:
    39
    that worked! Thanks guys! But how did that work as gameObject was never defined anywhere. I didn't think that the code would know that gameObject was tied to either GameObject or Player. I know it's case sensitive, but I figured if I changed the case of something that was already defined, it wouldn't know what to match it up with.

    and why would the other code not work? That way I don't make these mistakes again
     
    Last edited: Apr 9, 2015
  10. oscaro

    oscaro

    Joined:
    Apr 7, 2015
    Posts:
    18
    I don't properly know myself, I'm pretty sure what James said above is what you're looking for. GameObject is the base class for all entities in Unity scenes. gameObject is the actual object though? I think. Don't quote me on it.
     
  11. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Your class is derived from another class, which knows it.