Search Unity

OOP on Enemy Characters

Discussion in '2D' started by limbubish101, Mar 14, 2019 at 4:31 PM.

  1. limbubish101

    limbubish101

    Joined:
    Thursday
    Posts:
    1
    Here is my code for an enemy in the game. Right now its in development so there are no spawners, however it should just appear in the game as I have inserted him into the scene. However, when I play test the enemy disappears? I have no idea whats going on.




    Code (CSharp):
    1. public class badguy : MonoBehaviour
    2. {
    3.  
    4.     public class Badguys
    5.     {
    6.         public int health;
    7.         public float speed;
    8.         public float distance;
    9.         public int points;
    10.         public Transform target;
    11.     }
    12.  
    13.     Badguys Lanky = new Badguys();
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         Lanky.target = GameObject.FindGameObjectWithTag("player").GetComponent<Transform>();
    19.         Debug.Log("loaded");
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (Vector2.Distance(transform.position, Lanky.target.position) > Lanky.distance)
    26.         {
    27.             transform.position = Vector2.MoveTowards(transform.position, Lanky.target.position, Lanky.speed * Time.deltaTime);
    28.  
    29.         }
    30.  
    31.  
    32.         if (Lanky.health <= 0)
    33.         {
    34.             Destroy(gameObject);
    35.         }
    36.            
    37.     }
    38.  
    39.  
    40.     private void OnTriggerEnter2D(Collider2D col)
    41.     {
    42.         Hleath.health -= 1;
    43.         Debug.Log("die player");
    44.     }
    45.  
    46.     public void hit(int damage)
    47.     {
    48.         Lanky.health -= damage;
    49.         Debug.Log("i took damage");
    50.     }
    51. }
    52.  
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    713
    When you play test, can you still see the enemy listed in the hierarchy panel? If not, then something deleted the enemy game object.

    If it is there, it could be that the enemy is outside the camera view or behind something. Pause the game and switch to the scene view. Select the enemy in the hierarchy panel and ,with your mouse cursor over the scene view, press "f" on the keyboard. This will make the scene view camera focus on your enemy so you can see where it actually is.
     
    cscox2191 likes this.
  3. cscox2191

    cscox2191

    Joined:
    Feb 8, 2018
    Posts:
    31
    I would check what kdgalla said as it is probably something in the Unity editor or another script. Also, I noticed your OnTriggerEnter2D function. This won't work as you aren't actually checking to see if it hits the player. throw in an IF statement under the TriggerEnter function like: if(col.gameObject.tag == "player") and then have the 2 lines of code removing health.
     
  4. Spoin

    Spoin

    Joined:
    Friday
    Posts:
    1
    The value of the health field is initialized with zero, by default. Thus, your health check in line 32 evaluates immediately to true and the gameObject is removed on the first update call.

    My suggestion is to explicitly initialize health with some positive value.

    Hope that helps.